\hypertarget{namespacemom__set__visc}{}\doxysection{mom\+\_\+set\+\_\+visc Module Reference}
\label{namespacemom__set__visc}\index{mom\_set\_visc@{mom\_set\_visc}}


\doxysubsection{Detailed Description}
Calculates various values related to the bottom boundary layer, such as the viscosity and thickness of the B\+BL (set\+\_\+viscous\+\_\+\+B\+BL). 

This would also be the module in which other viscous quantities that are flow-\/independent might be set. This information is transmitted to other modules via a vertvisc type structure.

The same code is used for the two velocity components, by indirectly referencing the velocities and defining a handful of direction-\/specific defined variables. \doxysubsection*{Data Types}
\begin{DoxyCompactItemize}
\item 
type \mbox{\hyperlink{structmom__set__visc_1_1set__visc__cs}{set\+\_\+visc\+\_\+cs}}
\begin{DoxyCompactList}\small\item\em Control structure for M\+O\+M\+\_\+set\+\_\+visc. \end{DoxyCompactList}\end{DoxyCompactItemize}
\doxysubsection*{Functions/\+Subroutines}
\begin{DoxyCompactItemize}
\item 
subroutine, public \mbox{\hyperlink{namespacemom__set__visc_a9865113fe07928e7a240c2868ed45e5f}{set\+\_\+viscous\+\_\+bbl}} (u, v, h, tv, visc, G, GV, US, CS, symmetrize)
\begin{DoxyCompactList}\small\item\em Calculates the thickness of the bottom boundary layer and the viscosity within that layer. \end{DoxyCompactList}\item 
real function \mbox{\hyperlink{namespacemom__set__visc_a0356a4e81cca9f7f31bbf87c717a6600}{set\+\_\+v\+\_\+at\+\_\+u}} (v, h, G, i, j, k, mask2d\+Cv, O\+BC)
\begin{DoxyCompactList}\small\item\em This subroutine finds a thickness-\/weighted value of v at the u-\/points. \end{DoxyCompactList}\item 
real function \mbox{\hyperlink{namespacemom__set__visc_a46583b82467e74d8654c3c0a037a25cd}{set\+\_\+u\+\_\+at\+\_\+v}} (u, h, G, i, j, k, mask2d\+Cu, O\+BC)
\begin{DoxyCompactList}\small\item\em This subroutine finds a thickness-\/weighted value of u at the v-\/points. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__set__visc_aba41cd4f8baa1cda9036d97087ce8a22}{set\+\_\+viscous\+\_\+ml}} (u, v, h, tv, forces, visc, dt, G, GV, US, CS, symmetrize)
\begin{DoxyCompactList}\small\item\em Calculates the thickness of the surface boundary layer for applying an elevated viscosity. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__set__visc_ae2d9d9f74c1e9aec56257cfad372b0fd}{set\+\_\+visc\+\_\+register\+\_\+restarts}} (HI, GV, param\+\_\+file, visc, restart\+\_\+\+CS)
\begin{DoxyCompactList}\small\item\em Register any fields associated with the vertvisc\+\_\+type. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__set__visc_a2ec9be1a61c7d4a062aab4ff03e3ca29}{set\+\_\+visc\+\_\+init}} (Time, G, GV, US, param\+\_\+file, diag, visc, CS, restart\+\_\+\+CS, O\+BC)
\begin{DoxyCompactList}\small\item\em Initializes the M\+O\+M\+\_\+set\+\_\+visc control structure. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__set__visc_abbac7a7410557759c74444787de415a9}{set\+\_\+visc\+\_\+end}} (visc, CS)
\begin{DoxyCompactList}\small\item\em This subroutine dellocates any memory in the set\+\_\+visc control structure. \end{DoxyCompactList}\end{DoxyCompactItemize}


\doxysubsection{Function/\+Subroutine Documentation}
\mbox{\Hypertarget{namespacemom__set__visc_a46583b82467e74d8654c3c0a037a25cd}\label{namespacemom__set__visc_a46583b82467e74d8654c3c0a037a25cd}} 
\index{mom\_set\_visc@{mom\_set\_visc}!set\_u\_at\_v@{set\_u\_at\_v}}
\index{set\_u\_at\_v@{set\_u\_at\_v}!mom\_set\_visc@{mom\_set\_visc}}
\doxysubsubsection{\texorpdfstring{set\_u\_at\_v()}{set\_u\_at\_v()}}
{\footnotesize\ttfamily real function mom\+\_\+set\+\_\+visc\+::set\+\_\+u\+\_\+at\+\_\+v (\begin{DoxyParamCaption}\item[{real, dimension( g \%isdb\+: g \%iedb, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{u,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{h,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{integer, intent(in)}]{i,  }\item[{integer, intent(in)}]{j,  }\item[{integer, intent(in)}]{k,  }\item[{real, dimension( g \%isdb\+: g \%iedb, g \%jsd\+: g \%jed), intent(in)}]{mask2d\+Cu,  }\item[{type(ocean\+\_\+obc\+\_\+type), pointer}]{O\+BC }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine finds a thickness-\/weighted value of u at the v-\/points. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & The ocean\textquotesingle{}s grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em u} & The zonal velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]} or other units. \\
\hline
\mbox{\texttt{ in}}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em i} & The i-\/index of the u-\/location to work on. \\
\hline
\mbox{\texttt{ in}}  & {\em j} & The j-\/index of the u-\/location to work on. \\
\hline
\mbox{\texttt{ in}}  & {\em k} & The k-\/index of the u-\/location to work on. \\
\hline
\mbox{\texttt{ in}}  & {\em mask2dcu} & A multiplicative mask of the u-\/points \\
\hline
 & {\em obc} & A pointer to an open boundary condition structure \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
The return value of u at v points in the same units as u, i.\+e. \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]} or other units. 
\end{DoxyReturn}


Definition at line 1159 of file M\+O\+M\+\_\+set\+\_\+viscosity.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1160   \textcolor{keywordtype}{type}(ocean\_grid\_type),                     \textcolor{keywordtype}{intent(in)} :: G\textcolor{comment}{    !< The ocean's grid structure}}
\DoxyCodeLine{1161 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{1162                          \textcolor{keywordtype}{intent(in)} :: u\textcolor{comment}{    !< The zonal velocity [L T-\/1 \string~> m s-\/1] or other units.}}
\DoxyCodeLine{1163 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{1164                          \textcolor{keywordtype}{intent(in)} :: h\textcolor{comment}{    !< Layer thicknesses [H \string~> m or kg m-\/2]}}
\DoxyCodeLine{1165   \textcolor{keywordtype}{integer},               \textcolor{keywordtype}{intent(in)} :: i\textcolor{comment}{    !< The i-\/index of the u-\/location to work on.}}
\DoxyCodeLine{1166   \textcolor{keywordtype}{integer},               \textcolor{keywordtype}{intent(in)} :: j\textcolor{comment}{    !< The j-\/index of the u-\/location to work on.}}
\DoxyCodeLine{1167   \textcolor{keywordtype}{integer},               \textcolor{keywordtype}{intent(in)} :: k\textcolor{comment}{    !< The k-\/index of the u-\/location to work on.}}
\DoxyCodeLine{1168 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G))}, \&}
\DoxyCodeLine{1169                          \textcolor{keywordtype}{intent(in)} :: mask2dCu\textcolor{comment}{ !< A multiplicative mask of the u-\/points}}
\DoxyCodeLine{1170   \textcolor{keywordtype}{type}(ocean\_OBC\_type),  \textcolor{keywordtype}{pointer}    :: OBC\textcolor{comment}{  !< A pointer to an open boundary condition structure}}
\DoxyCodeLine{1171 \textcolor{keywordtype}{  real}                              :: set\_u\_at\_v\textcolor{comment}{ !< The return value of u at v points in the}}
\DoxyCodeLine{1172 \textcolor{comment}{                                            !! same units as u, i.e. [L T-\/1 \string~> m s-\/1] or other units.}}
\DoxyCodeLine{1173 }
\DoxyCodeLine{1174   \textcolor{comment}{! This subroutine finds a thickness-\/weighted value of u at the v-\/points.}}
\DoxyCodeLine{1175 \textcolor{keywordtype}{  real} :: hwt(-\/1:0,0:1)    \textcolor{comment}{! Masked weights used to average u onto v [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1176 \textcolor{keywordtype}{  real} :: hwt\_tot          \textcolor{comment}{! The sum of the masked thicknesses [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1177   \textcolor{keywordtype}{integer} :: i0, j0, i1, j1}
\DoxyCodeLine{1178 }
\DoxyCodeLine{1179   \textcolor{keywordflow}{do} j0 = 0,1 ; \textcolor{keywordflow}{do} i0 = -\/1,0 ; i1 = i+i0 ; j1 = j+j0}
\DoxyCodeLine{1180     hwt(i0,j0) = (h(i1,j1,k) + h(i1+1,j1,k)) * mask2dcu(i1,j1)}
\DoxyCodeLine{1181 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1182 }
\DoxyCodeLine{1183   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(obc)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (obc\%number\_of\_segments > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1184     \textcolor{keywordflow}{do} j0 = 0,1 ; \textcolor{keywordflow}{do} i0 = -\/1,0 ; \textcolor{keywordflow}{if} ((obc\%segnum\_u(i+i0,j+j0) /= obc\_none)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1185       i1 = i+i0 ; j1 = j+j0}
\DoxyCodeLine{1186       \textcolor{keywordflow}{if} (obc\%segment(obc\%segnum\_u(i1,j1))\%direction == obc\_direction\_e) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1187         hwt(i0,j0) = 2.0 * h(i1,j1,k) * mask2dcu(i1,j1)}
\DoxyCodeLine{1188       \textcolor{keywordflow}{elseif} (obc\%segment(obc\%segnum\_u(i1,j1))\%direction == obc\_direction\_w) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1189         hwt(i0,j0) = 2.0 * h(i1+1,j1,k) * mask2dcu(i1,j1)}
\DoxyCodeLine{1190 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1191 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1192 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1193 }
\DoxyCodeLine{1194   hwt\_tot = (hwt(-\/1,0) + hwt(0,1)) + (hwt(0,0) + hwt(-\/1,1))}
\DoxyCodeLine{1195   set\_u\_at\_v = 0.0}
\DoxyCodeLine{1196   \textcolor{keywordflow}{if} (hwt\_tot > 0.0) set\_u\_at\_v = \&}
\DoxyCodeLine{1197           ((hwt(0,0) * u(i,j,k) + hwt(-\/1,1) * u(i-\/1,j+1,k)) + \&}
\DoxyCodeLine{1198            (hwt(-\/1,0) * u(i-\/1,j,k) + hwt(0,1) * u(i,j+1,k))) / hwt\_tot}
\DoxyCodeLine{1199 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__set__visc_a0356a4e81cca9f7f31bbf87c717a6600}\label{namespacemom__set__visc_a0356a4e81cca9f7f31bbf87c717a6600}} 
\index{mom\_set\_visc@{mom\_set\_visc}!set\_v\_at\_u@{set\_v\_at\_u}}
\index{set\_v\_at\_u@{set\_v\_at\_u}!mom\_set\_visc@{mom\_set\_visc}}
\doxysubsubsection{\texorpdfstring{set\_v\_at\_u()}{set\_v\_at\_u()}}
{\footnotesize\ttfamily real function mom\+\_\+set\+\_\+visc\+::set\+\_\+v\+\_\+at\+\_\+u (\begin{DoxyParamCaption}\item[{real, dimension(szi\+\_\+(g),szjb\+\_\+(g),szk\+\_\+(g)), intent(in)}]{v,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{h,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{integer, intent(in)}]{i,  }\item[{integer, intent(in)}]{j,  }\item[{integer, intent(in)}]{k,  }\item[{real, dimension(szi\+\_\+(g),szjb\+\_\+(g)), intent(in)}]{mask2d\+Cv,  }\item[{type(ocean\+\_\+obc\+\_\+type), pointer}]{O\+BC }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine finds a thickness-\/weighted value of v at the u-\/points. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & The ocean\textquotesingle{}s grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em v} & The meridional velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em i} & The i-\/index of the u-\/location to work on. \\
\hline
\mbox{\texttt{ in}}  & {\em j} & The j-\/index of the u-\/location to work on. \\
\hline
\mbox{\texttt{ in}}  & {\em k} & The k-\/index of the u-\/location to work on. \\
\hline
\mbox{\texttt{ in}}  & {\em mask2dcv} & A multiplicative mask of the v-\/points \\
\hline
 & {\em obc} & A pointer to an open boundary condition structure \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
The return value of v at u points points in the same units as u, i.\+e. \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]} or other units. 
\end{DoxyReturn}


Definition at line 1115 of file M\+O\+M\+\_\+set\+\_\+viscosity.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1116   \textcolor{keywordtype}{type}(ocean\_grid\_type), \textcolor{keywordtype}{intent(in)} :: G\textcolor{comment}{    !< The ocean's grid structure}}
\DoxyCodeLine{1117 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{1118                          \textcolor{keywordtype}{intent(in)} :: v\textcolor{comment}{    !< The meridional velocity [L T-\/1 \string~> m s-\/1]}}
\DoxyCodeLine{1119 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{1120                          \textcolor{keywordtype}{intent(in)} :: h\textcolor{comment}{    !< Layer thicknesses [H \string~> m or kg m-\/2]}}
\DoxyCodeLine{1121   \textcolor{keywordtype}{integer},               \textcolor{keywordtype}{intent(in)} :: i\textcolor{comment}{    !< The i-\/index of the u-\/location to work on.}}
\DoxyCodeLine{1122   \textcolor{keywordtype}{integer},               \textcolor{keywordtype}{intent(in)} :: j\textcolor{comment}{    !< The j-\/index of the u-\/location to work on.}}
\DoxyCodeLine{1123   \textcolor{keywordtype}{integer},               \textcolor{keywordtype}{intent(in)} :: k\textcolor{comment}{    !< The k-\/index of the u-\/location to work on.}}
\DoxyCodeLine{1124 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G))},\&}
\DoxyCodeLine{1125                          \textcolor{keywordtype}{intent(in)} :: mask2dCv\textcolor{comment}{ !< A multiplicative mask of the v-\/points}}
\DoxyCodeLine{1126   \textcolor{keywordtype}{type}(ocean\_OBC\_type),  \textcolor{keywordtype}{pointer}    :: OBC\textcolor{comment}{  !< A pointer to an open boundary condition structure}}
\DoxyCodeLine{1127 \textcolor{keywordtype}{  real}                              :: set\_v\_at\_u\textcolor{comment}{ !< The return value of v at u points points in the}}
\DoxyCodeLine{1128 \textcolor{comment}{                                            !! same units as u, i.e. [L T-\/1 \string~> m s-\/1] or other units.}}
\DoxyCodeLine{1129 }
\DoxyCodeLine{1130   \textcolor{comment}{! This subroutine finds a thickness-\/weighted value of v at the u-\/points.}}
\DoxyCodeLine{1131 \textcolor{keywordtype}{  real} :: hwt(0:1,-\/1:0)    \textcolor{comment}{! Masked weights used to average u onto v [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1132 \textcolor{keywordtype}{  real} :: hwt\_tot          \textcolor{comment}{! The sum of the masked thicknesses [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1133   \textcolor{keywordtype}{integer} :: i0, j0, i1, j1}
\DoxyCodeLine{1134 }
\DoxyCodeLine{1135   \textcolor{keywordflow}{do} j0 = -\/1,0 ; \textcolor{keywordflow}{do} i0 = 0,1 ; i1 = i+i0 ; j1 = j+j0}
\DoxyCodeLine{1136     hwt(i0,j0) = (h(i1,j1,k) + h(i1,j1+1,k)) * mask2dcv(i1,j1)}
\DoxyCodeLine{1137 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1138 }
\DoxyCodeLine{1139   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(obc)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (obc\%number\_of\_segments > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1140     \textcolor{keywordflow}{do} j0 = -\/1,0 ; \textcolor{keywordflow}{do} i0 = 0,1 ; \textcolor{keywordflow}{if} ((obc\%segnum\_v(i+i0,j+j0) /= obc\_none)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1141       i1 = i+i0 ; j1 = j+j0}
\DoxyCodeLine{1142       \textcolor{keywordflow}{if} (obc\%segment(obc\%segnum\_v(i1,j1))\%direction == obc\_direction\_n) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1143         hwt(i0,j0) = 2.0 * h(i1,j1,k) * mask2dcv(i1,j1)}
\DoxyCodeLine{1144       \textcolor{keywordflow}{elseif} (obc\%segment(obc\%segnum\_v(i1,j1))\%direction == obc\_direction\_s) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1145         hwt(i0,j0) = 2.0 * h(i1,j1+1,k) * mask2dcv(i1,j1)}
\DoxyCodeLine{1146 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1147 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1148 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1149 }
\DoxyCodeLine{1150   hwt\_tot = (hwt(0,-\/1) + hwt(1,0)) + (hwt(1,-\/1) + hwt(0,0))}
\DoxyCodeLine{1151   set\_v\_at\_u = 0.0}
\DoxyCodeLine{1152   \textcolor{keywordflow}{if} (hwt\_tot > 0.0) set\_v\_at\_u = \&}
\DoxyCodeLine{1153           ((hwt(0,0) * v(i,j,k) + hwt(1,-\/1) * v(i+1,j-\/1,k)) + \&}
\DoxyCodeLine{1154            (hwt(1,0) * v(i+1,j,k) + hwt(0,-\/1) * v(i,j-\/1,k))) / hwt\_tot}
\DoxyCodeLine{1155 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__set__visc_abbac7a7410557759c74444787de415a9}\label{namespacemom__set__visc_abbac7a7410557759c74444787de415a9}} 
\index{mom\_set\_visc@{mom\_set\_visc}!set\_visc\_end@{set\_visc\_end}}
\index{set\_visc\_end@{set\_visc\_end}!mom\_set\_visc@{mom\_set\_visc}}
\doxysubsubsection{\texorpdfstring{set\_visc\_end()}{set\_visc\_end()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+set\+\_\+visc\+::set\+\_\+visc\+\_\+end (\begin{DoxyParamCaption}\item[{type(vertvisc\+\_\+type), intent(inout)}]{visc,  }\item[{type(\mbox{\hyperlink{structmom__set__visc_1_1set__visc__cs}{set\+\_\+visc\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



This subroutine dellocates any memory in the set\+\_\+visc control structure. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in,out}}  & {\em visc} & A structure containing vertical viscosities and related fields. Elements are deallocated here. \\
\hline
 & {\em cs} & The control structure returned by a previous call to set\+\_\+visc\+\_\+init. \\
\hline
\end{DoxyParams}


Definition at line 2314 of file M\+O\+M\+\_\+set\+\_\+viscosity.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{2315   \textcolor{keywordtype}{type}(vertvisc\_type), \textcolor{keywordtype}{intent(inout)} :: visc\textcolor{comment}{ !< A structure containing vertical viscosities and}}
\DoxyCodeLine{2316 \textcolor{comment}{                                             !! related fields.  Elements are deallocated here.}}
\DoxyCodeLine{2317   \textcolor{keywordtype}{type}(set\_visc\_CS),   \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{   !< The control structure returned by a previous}}
\DoxyCodeLine{2318 \textcolor{comment}{                                             !! call to set\_visc\_init.}}
\DoxyCodeLine{2319   \textcolor{keywordflow}{if} (cs\%bottomdraglaw) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2320     \textcolor{keyword}{deallocate}(visc\%bbl\_thick\_u) ; \textcolor{keyword}{deallocate}(visc\%bbl\_thick\_v)}
\DoxyCodeLine{2321     \textcolor{keyword}{deallocate}(visc\%kv\_bbl\_u) ; \textcolor{keyword}{deallocate}(visc\%kv\_bbl\_v)}
\DoxyCodeLine{2322     \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs\%bbl\_u)) \textcolor{keyword}{deallocate}(cs\%bbl\_u)}
\DoxyCodeLine{2323     \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs\%bbl\_v)) \textcolor{keyword}{deallocate}(cs\%bbl\_v)}
\DoxyCodeLine{2324 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2325   \textcolor{keywordflow}{if} (cs\%Channel\_drag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2326     \textcolor{keyword}{deallocate}(visc\%Ray\_u) ; \textcolor{keyword}{deallocate}(visc\%Ray\_v)}
\DoxyCodeLine{2327 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2328   \textcolor{keywordflow}{if} (cs\%dynamic\_viscous\_ML) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2329     \textcolor{keyword}{deallocate}(visc\%nkml\_visc\_u) ; \textcolor{keyword}{deallocate}(visc\%nkml\_visc\_v)}
\DoxyCodeLine{2330 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2331   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%Kd\_shear)) \textcolor{keyword}{deallocate}(visc\%Kd\_shear)}
\DoxyCodeLine{2332   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%Kv\_slow)) \textcolor{keyword}{deallocate}(visc\%Kv\_slow)}
\DoxyCodeLine{2333   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%TKE\_turb)) \textcolor{keyword}{deallocate}(visc\%TKE\_turb)}
\DoxyCodeLine{2334   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%Kv\_shear)) \textcolor{keyword}{deallocate}(visc\%Kv\_shear)}
\DoxyCodeLine{2335   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%Kv\_shear\_Bu)) \textcolor{keyword}{deallocate}(visc\%Kv\_shear\_Bu)}
\DoxyCodeLine{2336   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%ustar\_bbl)) \textcolor{keyword}{deallocate}(visc\%ustar\_bbl)}
\DoxyCodeLine{2337   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%TKE\_bbl)) \textcolor{keyword}{deallocate}(visc\%TKE\_bbl)}
\DoxyCodeLine{2338   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%taux\_shelf)) \textcolor{keyword}{deallocate}(visc\%taux\_shelf)}
\DoxyCodeLine{2339   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%tauy\_shelf)) \textcolor{keyword}{deallocate}(visc\%tauy\_shelf)}
\DoxyCodeLine{2340   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%tbl\_thick\_shelf\_u)) \textcolor{keyword}{deallocate}(visc\%tbl\_thick\_shelf\_u)}
\DoxyCodeLine{2341   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%tbl\_thick\_shelf\_v)) \textcolor{keyword}{deallocate}(visc\%tbl\_thick\_shelf\_v)}
\DoxyCodeLine{2342   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%kv\_tbl\_shelf\_u)) \textcolor{keyword}{deallocate}(visc\%kv\_tbl\_shelf\_u)}
\DoxyCodeLine{2343   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%kv\_tbl\_shelf\_v)) \textcolor{keyword}{deallocate}(visc\%kv\_tbl\_shelf\_v)}
\DoxyCodeLine{2344 }
\DoxyCodeLine{2345   \textcolor{keyword}{deallocate}(cs)}

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__set__visc_a2ec9be1a61c7d4a062aab4ff03e3ca29}\label{namespacemom__set__visc_a2ec9be1a61c7d4a062aab4ff03e3ca29}} 
\index{mom\_set\_visc@{mom\_set\_visc}!set\_visc\_init@{set\_visc\_init}}
\index{set\_visc\_init@{set\_visc\_init}!mom\_set\_visc@{mom\_set\_visc}}
\doxysubsubsection{\texorpdfstring{set\_visc\_init()}{set\_visc\_init()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+set\+\_\+visc\+::set\+\_\+visc\+\_\+init (\begin{DoxyParamCaption}\item[{type(time\+\_\+type), intent(in), target}]{Time,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(inout)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(param\+\_\+file\+\_\+type), intent(in)}]{param\+\_\+file,  }\item[{type(diag\+\_\+ctrl), intent(inout), target}]{diag,  }\item[{type(vertvisc\+\_\+type), intent(inout)}]{visc,  }\item[{type(\mbox{\hyperlink{structmom__set__visc_1_1set__visc__cs}{set\+\_\+visc\+\_\+cs}}), pointer}]{CS,  }\item[{type(mom\+\_\+restart\+\_\+cs), pointer}]{restart\+\_\+\+CS,  }\item[{type(ocean\+\_\+obc\+\_\+type), pointer}]{O\+BC }\end{DoxyParamCaption})}



Initializes the M\+O\+M\+\_\+set\+\_\+visc control structure. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em time} & The current model time. \\
\hline
\mbox{\texttt{ in,out}}  & {\em g} & The ocean\textquotesingle{}s grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in}}  & {\em param\+\_\+file} & A structure to parse for run-\/time parameters. \\
\hline
\mbox{\texttt{ in,out}}  & {\em diag} & A structure that is used to regulate diagnostic output. \\
\hline
\mbox{\texttt{ in,out}}  & {\em visc} & A structure containing vertical viscosities and related fields. Allocated here. \\
\hline
 & {\em cs} & A pointer that is set to point to the control structure for this module \\
\hline
 & {\em restart\+\_\+cs} & A pointer to the restart control structure. \\
\hline
 & {\em obc} & A pointer to an open boundary condition structure \\
\hline
\end{DoxyParams}


Definition at line 1972 of file M\+O\+M\+\_\+set\+\_\+viscosity.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1973   \textcolor{keywordtype}{type}(time\_type), \textcolor{keywordtype}{target}, \textcolor{keywordtype}{intent(in)}    :: Time\textcolor{comment}{ !< The current model time.}}
\DoxyCodeLine{1974   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(inout)} :: G\textcolor{comment}{    !< The ocean's grid structure.}}
\DoxyCodeLine{1975   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure.}}
\DoxyCodeLine{1976   \textcolor{keywordtype}{type}(unit\_scale\_type),   \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{   !< A dimensional unit scaling type}}
\DoxyCodeLine{1977   \textcolor{keywordtype}{type}(param\_file\_type),   \textcolor{keywordtype}{intent(in)}    :: param\_file\textcolor{comment}{ !< A structure to parse for run-\/time}}
\DoxyCodeLine{1978 \textcolor{comment}{                                                 !! parameters.}}
\DoxyCodeLine{1979   \textcolor{keywordtype}{type}(diag\_ctrl), \textcolor{keywordtype}{target}, \textcolor{keywordtype}{intent(inout)} :: diag\textcolor{comment}{ !< A structure that is used to regulate diagnostic}}
\DoxyCodeLine{1980 \textcolor{comment}{                                                 !! output.}}
\DoxyCodeLine{1981   \textcolor{keywordtype}{type}(vertvisc\_type),     \textcolor{keywordtype}{intent(inout)} :: visc\textcolor{comment}{ !< A structure containing vertical viscosities and}}
\DoxyCodeLine{1982 \textcolor{comment}{                                                 !! related fields.  Allocated here.}}
\DoxyCodeLine{1983   \textcolor{keywordtype}{type}(set\_visc\_CS),       \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{   !< A pointer that is set to point to the control}}
\DoxyCodeLine{1984 \textcolor{comment}{                                                 !! structure for this module}}
\DoxyCodeLine{1985   \textcolor{keywordtype}{type}(MOM\_restart\_CS),    \textcolor{keywordtype}{pointer}       :: restart\_CS\textcolor{comment}{ !< A pointer to the restart control structure.}}
\DoxyCodeLine{1986   \textcolor{keywordtype}{type}(ocean\_OBC\_type),    \textcolor{keywordtype}{pointer}       :: OBC\textcolor{comment}{  !< A pointer to an open boundary condition structure}}
\DoxyCodeLine{1987 }
\DoxyCodeLine{1988   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{1989 \textcolor{keywordtype}{  real}    :: Csmag\_chan\_dflt, smag\_const1, TKE\_decay\_dflt, bulk\_Ri\_ML\_dflt}
\DoxyCodeLine{1990 \textcolor{keywordtype}{  real}    :: Kv\_background}
\DoxyCodeLine{1991 \textcolor{keywordtype}{  real}    :: omega\_frac\_dflt}
\DoxyCodeLine{1992 \textcolor{keywordtype}{  real}    :: Z\_rescale     \textcolor{comment}{! A rescaling factor for heights from the representation in}}
\DoxyCodeLine{1993                            \textcolor{comment}{! a restart file to the internal representation in this run.}}
\DoxyCodeLine{1994 \textcolor{keywordtype}{  real}    :: I\_T\_rescale   \textcolor{comment}{! A rescaling factor for time from the internal representation in this run}}
\DoxyCodeLine{1995                            \textcolor{comment}{! to the representation in a restart file.}}
\DoxyCodeLine{1996 \textcolor{keywordtype}{  real}    :: Z2\_T\_rescale  \textcolor{comment}{! A rescaling factor for vertical diffusivities and viscosities from the}}
\DoxyCodeLine{1997                            \textcolor{comment}{! representation in a restart file to the internal representation in this run.}}
\DoxyCodeLine{1998   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, n}
\DoxyCodeLine{1999   \textcolor{keywordtype}{integer} :: isd, ied, jsd, jed, IsdB, IedB, JsdB, JedB, nz}
\DoxyCodeLine{2000   \textcolor{keywordtype}{logical} :: default\_2018\_answers}
\DoxyCodeLine{2001   \textcolor{keywordtype}{logical} :: use\_kappa\_shear, adiabatic, use\_omega, MLE\_use\_PBL\_MLD}
\DoxyCodeLine{2002   \textcolor{keywordtype}{logical} :: use\_KPP}
\DoxyCodeLine{2003   \textcolor{keywordtype}{logical} :: use\_regridding}
\DoxyCodeLine{2004   \textcolor{keywordtype}{character(len=200)} :: filename, tideamp\_file}
\DoxyCodeLine{2005   \textcolor{keywordtype}{type}(OBC\_segment\_type), \textcolor{keywordtype}{pointer} :: segment => null() \textcolor{comment}{! pointer to OBC segment type}}
\DoxyCodeLine{2006   \textcolor{comment}{! This include declares and sets the variable "version".}}
\DoxyCodeLine{2007 \textcolor{preprocessor}{\# include "version\_variable.h"}}
\DoxyCodeLine{2008 \textcolor{preprocessor}{}  \textcolor{keywordtype}{character(len=40)}  :: mdl = \textcolor{stringliteral}{"MOM\_set\_visc"}  \textcolor{comment}{! This module's name.}}
\DoxyCodeLine{2009 }
\DoxyCodeLine{2010   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2011     \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"set\_visc\_init called with an associated "}// \&}
\DoxyCodeLine{2012                             \textcolor{stringliteral}{"control structure."})}
\DoxyCodeLine{2013     \textcolor{keywordflow}{return}}
\DoxyCodeLine{2014 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2015   \textcolor{keyword}{allocate}(cs)}
\DoxyCodeLine{2016 }
\DoxyCodeLine{2017   cs\%OBC => obc}
\DoxyCodeLine{2018 }
\DoxyCodeLine{2019   is = g\%isc ; ie = g\%iec ; js = g\%jsc ; je = g\%jec}
\DoxyCodeLine{2020   isd = g\%isd ; ied = g\%ied ; jsd = g\%jsd ; jed = g\%jed ; nz = gv\%ke}
\DoxyCodeLine{2021   isdb = g\%IsdB ; iedb = g\%IedB ; jsdb = g\%JsdB ; jedb = g\%JedB}
\DoxyCodeLine{2022 }
\DoxyCodeLine{2023   cs\%diag => diag}
\DoxyCodeLine{2024 }
\DoxyCodeLine{2025   \textcolor{comment}{! Set default, read and log parameters}}
\DoxyCodeLine{2026   \textcolor{keyword}{call }log\_version(param\_file, mdl, version, \textcolor{stringliteral}{""})}
\DoxyCodeLine{2027   cs\%RiNo\_mix = .false.}
\DoxyCodeLine{2028   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"INPUTDIR"}, cs\%inputdir, default=\textcolor{stringliteral}{"."})}
\DoxyCodeLine{2029   cs\%inputdir = slasher(cs\%inputdir)}
\DoxyCodeLine{2030   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DEFAULT\_2018\_ANSWERS"}, default\_2018\_answers, \&}
\DoxyCodeLine{2031                  \textcolor{stringliteral}{"This sets the default value for the various \_2018\_ANSWERS parameters."}, \&}
\DoxyCodeLine{2032                  default=.false.)}
\DoxyCodeLine{2033   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"SET\_VISC\_2018\_ANSWERS"}, cs\%answers\_2018, \&}
\DoxyCodeLine{2034                  \textcolor{stringliteral}{"If true, use the order of arithmetic and expressions that recover the "}//\&}
\DoxyCodeLine{2035                  \textcolor{stringliteral}{"answers from the end of 2018.  Otherwise, use updated and more robust "}//\&}
\DoxyCodeLine{2036                  \textcolor{stringliteral}{"forms of the same expressions."}, default=default\_2018\_answers)}
\DoxyCodeLine{2037   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BOTTOMDRAGLAW"}, cs\%bottomdraglaw, \&}
\DoxyCodeLine{2038                  \textcolor{stringliteral}{"If true, the bottom stress is calculated with a drag "}//\&}
\DoxyCodeLine{2039                  \textcolor{stringliteral}{"law of the form c\_drag*|u|*u. The velocity magnitude "}//\&}
\DoxyCodeLine{2040                  \textcolor{stringliteral}{"may be an assumed value or it may be based on the "}//\&}
\DoxyCodeLine{2041                  \textcolor{stringliteral}{"actual velocity in the bottommost HBBL, depending on "}//\&}
\DoxyCodeLine{2042                  \textcolor{stringliteral}{"LINEAR\_DRAG."}, default=.true.)}
\DoxyCodeLine{2043   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"CHANNEL\_DRAG"}, cs\%Channel\_drag, \&}
\DoxyCodeLine{2044                  \textcolor{stringliteral}{"If true, the bottom drag is exerted directly on each "}//\&}
\DoxyCodeLine{2045                  \textcolor{stringliteral}{"layer proportional to the fraction of the bottom it "}//\&}
\DoxyCodeLine{2046                  \textcolor{stringliteral}{"overlies."}, default=.false.)}
\DoxyCodeLine{2047   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"LINEAR\_DRAG"}, cs\%linear\_drag, \&}
\DoxyCodeLine{2048                  \textcolor{stringliteral}{"If LINEAR\_DRAG and BOTTOMDRAGLAW are defined the drag "}//\&}
\DoxyCodeLine{2049                  \textcolor{stringliteral}{"law is cdrag*DRAG\_BG\_VEL*u."}, default=.false.)}
\DoxyCodeLine{2050   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ADIABATIC"}, adiabatic, default=.false., \&}
\DoxyCodeLine{2051                  do\_not\_log=.true.)}
\DoxyCodeLine{2052   \textcolor{keywordflow}{if} (adiabatic) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2053     \textcolor{keyword}{call }log\_param(param\_file, mdl, \textcolor{stringliteral}{"ADIABATIC"},adiabatic, \&}
\DoxyCodeLine{2054                  \textcolor{stringliteral}{"There are no diapycnal mass fluxes if ADIABATIC is "}//\&}
\DoxyCodeLine{2055                  \textcolor{stringliteral}{"true. This assumes that KD = KDML = 0.0 and that "}//\&}
\DoxyCodeLine{2056                  \textcolor{stringliteral}{"there is no buoyancy forcing, but makes the model "}//\&}
\DoxyCodeLine{2057                  \textcolor{stringliteral}{"faster by eliminating subroutine calls."}, default=.false.)}
\DoxyCodeLine{2058 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2059 }
\DoxyCodeLine{2060   \textcolor{keywordflow}{if} (.not.adiabatic) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2061     cs\%RiNo\_mix = kappa\_shear\_is\_used(param\_file)}
\DoxyCodeLine{2062 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2063 }
\DoxyCodeLine{2064   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"PRANDTL\_TURB"}, visc\%Prandtl\_turb, \&}
\DoxyCodeLine{2065                  \textcolor{stringliteral}{"The turbulent Prandtl number applied to shear "}//\&}
\DoxyCodeLine{2066                  \textcolor{stringliteral}{"instability."}, units=\textcolor{stringliteral}{"nondim"}, default=1.0)}
\DoxyCodeLine{2067   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DEBUG"}, cs\%debug, default=.false.)}
\DoxyCodeLine{2068 }
\DoxyCodeLine{2069   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DYNAMIC\_VISCOUS\_ML"}, cs\%dynamic\_viscous\_ML, \&}
\DoxyCodeLine{2070                  \textcolor{stringliteral}{"If true, use a bulk Richardson number criterion to "}//\&}
\DoxyCodeLine{2071                  \textcolor{stringliteral}{"determine the mixed layer thickness for viscosity."}, \&}
\DoxyCodeLine{2072                  default=.false.)}
\DoxyCodeLine{2073   \textcolor{keywordflow}{if} (cs\%dynamic\_viscous\_ML) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2074     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BULK\_RI\_ML"}, bulk\_ri\_ml\_dflt, default=0.0)}
\DoxyCodeLine{2075     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BULK\_RI\_ML\_VISC"}, cs\%bulk\_Ri\_ML, \&}
\DoxyCodeLine{2076                  \textcolor{stringliteral}{"The efficiency with which mean kinetic energy released "}//\&}
\DoxyCodeLine{2077                  \textcolor{stringliteral}{"by mechanically forced entrainment of the mixed layer "}//\&}
\DoxyCodeLine{2078                  \textcolor{stringliteral}{"is converted to turbulent kinetic energy.  By default, "}//\&}
\DoxyCodeLine{2079                  \textcolor{stringliteral}{"BULK\_RI\_ML\_VISC = BULK\_RI\_ML or 0."}, units=\textcolor{stringliteral}{"nondim"}, \&}
\DoxyCodeLine{2080                  default=bulk\_ri\_ml\_dflt)}
\DoxyCodeLine{2081     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"TKE\_DECAY"}, tke\_decay\_dflt, default=0.0)}
\DoxyCodeLine{2082     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"TKE\_DECAY\_VISC"}, cs\%TKE\_decay, \&}
\DoxyCodeLine{2083                  \textcolor{stringliteral}{"TKE\_DECAY\_VISC relates the vertical rate of decay of "}//\&}
\DoxyCodeLine{2084                  \textcolor{stringliteral}{"the TKE available for mechanical entrainment to the "}//\&}
\DoxyCodeLine{2085                  \textcolor{stringliteral}{"natural Ekman depth for use in calculating the dynamic "}//\&}
\DoxyCodeLine{2086                  \textcolor{stringliteral}{"mixed layer viscosity.  By default, "}//\&}
\DoxyCodeLine{2087                  \textcolor{stringliteral}{"TKE\_DECAY\_VISC = TKE\_DECAY or 0."}, units=\textcolor{stringliteral}{"nondim"}, \&}
\DoxyCodeLine{2088                  default=tke\_decay\_dflt)}
\DoxyCodeLine{2089     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_USE\_OMEGA"}, use\_omega, \&}
\DoxyCodeLine{2090                  \textcolor{stringliteral}{"If true, use the absolute rotation rate instead of the "}//\&}
\DoxyCodeLine{2091                  \textcolor{stringliteral}{"vertical component of rotation when setting the decay "}//\&}
\DoxyCodeLine{2092                    \textcolor{stringliteral}{"scale for turbulence."}, default=.false., do\_not\_log=.true.)}
\DoxyCodeLine{2093     omega\_frac\_dflt = 0.0}
\DoxyCodeLine{2094     \textcolor{keywordflow}{if} (use\_omega) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2095       \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"ML\_USE\_OMEGA is depricated; use ML\_OMEGA\_FRAC=1.0 instead."})}
\DoxyCodeLine{2096       omega\_frac\_dflt = 1.0}
\DoxyCodeLine{2097 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2098     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_OMEGA\_FRAC"}, cs\%omega\_frac, \&}
\DoxyCodeLine{2099                    \textcolor{stringliteral}{"When setting the decay scale for turbulence, use this "}//\&}
\DoxyCodeLine{2100                    \textcolor{stringliteral}{"fraction of the absolute rotation rate blended with the "}//\&}
\DoxyCodeLine{2101                    \textcolor{stringliteral}{"local value of f, as sqrt((1-\/of)*f\string^2 + of*4*omega\string^2)."}, \&}
\DoxyCodeLine{2102                    units=\textcolor{stringliteral}{"nondim"}, default=omega\_frac\_dflt)}
\DoxyCodeLine{2103     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"OMEGA"}, cs\%omega, \&}
\DoxyCodeLine{2104                  \textcolor{stringliteral}{"The rotation rate of the earth."}, units=\textcolor{stringliteral}{"s-\/1"}, \&}
\DoxyCodeLine{2105                  default=7.2921e-\/5, scale=us\%T\_to\_s)}
\DoxyCodeLine{2106     \textcolor{comment}{! This give a minimum decay scale that is typically much less than Angstrom.}}
\DoxyCodeLine{2107     cs\%ustar\_min = 2e-\/4*cs\%omega*(gv\%Angstrom\_Z + gv\%H\_to\_Z*gv\%H\_subroundoff)}
\DoxyCodeLine{2108   \textcolor{keywordflow}{else}}
\DoxyCodeLine{2109     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"OMEGA"}, cs\%omega, \&}
\DoxyCodeLine{2110                  \textcolor{stringliteral}{"The rotation rate of the earth."}, units=\textcolor{stringliteral}{"s-\/1"}, \&}
\DoxyCodeLine{2111                  default=7.2921e-\/5, scale=us\%T\_to\_s)}
\DoxyCodeLine{2112 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2113 }
\DoxyCodeLine{2114   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"HBBL"}, cs\%Hbbl, \&}
\DoxyCodeLine{2115                  \textcolor{stringliteral}{"The thickness of a bottom boundary layer with a "}//\&}
\DoxyCodeLine{2116                  \textcolor{stringliteral}{"viscosity of KVBBL if BOTTOMDRAGLAW is not defined, or "}//\&}
\DoxyCodeLine{2117                  \textcolor{stringliteral}{"the thickness over which near-\/bottom velocities are "}//\&}
\DoxyCodeLine{2118                  \textcolor{stringliteral}{"averaged for the drag law if BOTTOMDRAGLAW is defined "}//\&}
\DoxyCodeLine{2119                  \textcolor{stringliteral}{"but LINEAR\_DRAG is not."}, units=\textcolor{stringliteral}{"m"}, fail\_if\_missing=.true.) \textcolor{comment}{! Rescaled later}}
\DoxyCodeLine{2120   \textcolor{keywordflow}{if} (cs\%bottomdraglaw) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2121     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"CDRAG"}, cs\%cdrag, \&}
\DoxyCodeLine{2122                  \textcolor{stringliteral}{"CDRAG is the drag coefficient relating the magnitude of "}//\&}
\DoxyCodeLine{2123                  \textcolor{stringliteral}{"the velocity field to the bottom stress. CDRAG is only "}//\&}
\DoxyCodeLine{2124                  \textcolor{stringliteral}{"used if BOTTOMDRAGLAW is defined."}, units=\textcolor{stringliteral}{"nondim"}, \&}
\DoxyCodeLine{2125                  default=0.003)}
\DoxyCodeLine{2126     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BBL\_USE\_TIDAL\_BG"}, cs\%BBL\_use\_tidal\_bg, \&}
\DoxyCodeLine{2127                  \textcolor{stringliteral}{"Flag to use the tidal RMS amplitude in place of constant "}//\&}
\DoxyCodeLine{2128                  \textcolor{stringliteral}{"background velocity for computing u* in the BBL. "}//\&}
\DoxyCodeLine{2129                  \textcolor{stringliteral}{"This flag is only used when BOTTOMDRAGLAW is true and "}//\&}
\DoxyCodeLine{2130                  \textcolor{stringliteral}{"LINEAR\_DRAG is false."}, default=.false.)}
\DoxyCodeLine{2131     \textcolor{keywordflow}{if} (cs\%BBL\_use\_tidal\_bg) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2132       \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"TIDEAMP\_FILE"}, tideamp\_file, \&}
\DoxyCodeLine{2133                    \textcolor{stringliteral}{"The path to the file containing the spatially varying "}//\&}
\DoxyCodeLine{2134                    \textcolor{stringliteral}{"tidal amplitudes with INT\_TIDE\_DISSIPATION."}, default=\textcolor{stringliteral}{"tideamp.nc"})}
\DoxyCodeLine{2135     \textcolor{keywordflow}{else}}
\DoxyCodeLine{2136       \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DRAG\_BG\_VEL"}, cs\%drag\_bg\_vel, \&}
\DoxyCodeLine{2137                    \textcolor{stringliteral}{"DRAG\_BG\_VEL is either the assumed bottom velocity (with "}//\&}
\DoxyCodeLine{2138                    \textcolor{stringliteral}{"LINEAR\_DRAG) or an unresolved  velocity that is "}//\&}
\DoxyCodeLine{2139                    \textcolor{stringliteral}{"combined with the resolved velocity to estimate the "}//\&}
\DoxyCodeLine{2140                    \textcolor{stringliteral}{"velocity magnitude.  DRAG\_BG\_VEL is only used when "}//\&}
\DoxyCodeLine{2141                    \textcolor{stringliteral}{"BOTTOMDRAGLAW is defined."}, units=\textcolor{stringliteral}{"m s-\/1"}, default=0.0, scale=us\%m\_s\_to\_L\_T)}
\DoxyCodeLine{2142 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2143     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"USE\_REGRIDDING"}, use\_regridding, \&}
\DoxyCodeLine{2144          do\_not\_log = .true., default = .false. )}
\DoxyCodeLine{2145     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BBL\_USE\_EOS"}, cs\%BBL\_use\_EOS, \&}
\DoxyCodeLine{2146                  \textcolor{stringliteral}{"If true, use the equation of state in determining the "}//\&}
\DoxyCodeLine{2147                  \textcolor{stringliteral}{"properties of the bottom boundary layer.  Otherwise use "}//\&}
\DoxyCodeLine{2148                  \textcolor{stringliteral}{"the layer target potential densities.  The default of "}//\&}
\DoxyCodeLine{2149                  \textcolor{stringliteral}{"this is determined by USE\_REGRIDDING."}, default=use\_regridding)}
\DoxyCodeLine{2150     \textcolor{keywordflow}{if} (use\_regridding .and. (.not. cs\%BBL\_use\_EOS)) \&}
\DoxyCodeLine{2151       \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"When using MOM6 in ALE mode it is required to "}//\&}
\DoxyCodeLine{2152            \textcolor{stringliteral}{"set BBL\_USE\_EOS to True"})}
\DoxyCodeLine{2153 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2154   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BBL\_THICK\_MIN"}, cs\%BBL\_thick\_min, \&}
\DoxyCodeLine{2155                  \textcolor{stringliteral}{"The minimum bottom boundary layer thickness that can be "}//\&}
\DoxyCodeLine{2156                  \textcolor{stringliteral}{"used with BOTTOMDRAGLAW. This might be "}//\&}
\DoxyCodeLine{2157                  \textcolor{stringliteral}{"Kv/(cdrag*drag\_bg\_vel) to give Kv as the minimum "}//\&}
\DoxyCodeLine{2158                  \textcolor{stringliteral}{"near-\/bottom viscosity."}, units=\textcolor{stringliteral}{"m"}, default=0.0)  \textcolor{comment}{! Rescaled later}}
\DoxyCodeLine{2159   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"HTBL\_SHELF\_MIN"}, cs\%Htbl\_shelf\_min, \&}
\DoxyCodeLine{2160                  \textcolor{stringliteral}{"The minimum top boundary layer thickness that can be "}//\&}
\DoxyCodeLine{2161                  \textcolor{stringliteral}{"used with BOTTOMDRAGLAW. This might be "}//\&}
\DoxyCodeLine{2162                  \textcolor{stringliteral}{"Kv/(cdrag*drag\_bg\_vel) to give Kv as the minimum "}//\&}
\DoxyCodeLine{2163                  \textcolor{stringliteral}{"near-\/top viscosity."}, units=\textcolor{stringliteral}{"m"}, default=cs\%BBL\_thick\_min, scale=gv\%m\_to\_H)}
\DoxyCodeLine{2164   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"HTBL\_SHELF"}, cs\%Htbl\_shelf, \&}
\DoxyCodeLine{2165                  \textcolor{stringliteral}{"The thickness over which near-\/surface velocities are "}//\&}
\DoxyCodeLine{2166                  \textcolor{stringliteral}{"averaged for the drag law under an ice shelf.  By "}//\&}
\DoxyCodeLine{2167                  \textcolor{stringliteral}{"default this is the same as HBBL"}, units=\textcolor{stringliteral}{"m"}, default=cs\%Hbbl, scale=gv\%m\_to\_H)}
\DoxyCodeLine{2168   \textcolor{comment}{! These unit conversions are out outside the get\_param calls because the are also defaults.}}
\DoxyCodeLine{2169   cs\%Hbbl = cs\%Hbbl * gv\%m\_to\_H                   \textcolor{comment}{! Rescale}}
\DoxyCodeLine{2170   cs\%BBL\_thick\_min = cs\%BBL\_thick\_min * gv\%m\_to\_H \textcolor{comment}{! Rescale}}
\DoxyCodeLine{2171 }
\DoxyCodeLine{2172   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KV"}, kv\_background, \&}
\DoxyCodeLine{2173                  \textcolor{stringliteral}{"The background kinematic viscosity in the interior. "}//\&}
\DoxyCodeLine{2174                  \textcolor{stringliteral}{"The molecular value, \string~1e-\/6 m2 s-\/1, may be used."}, \&}
\DoxyCodeLine{2175                  units=\textcolor{stringliteral}{"m2 s-\/1"}, fail\_if\_missing=.true.)}
\DoxyCodeLine{2176 }
\DoxyCodeLine{2177   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"USE\_KPP"}, use\_kpp, \&}
\DoxyCodeLine{2178                  \textcolor{stringliteral}{"If true, turns on the [CVMix] KPP scheme of Large et al., 1994, "}//\&}
\DoxyCodeLine{2179                  \textcolor{stringliteral}{"to calculate diffusivities and non-\/local transport in the OBL."}, \&}
\DoxyCodeLine{2180                  do\_not\_log=.true., default=.false.)}
\DoxyCodeLine{2181 }
\DoxyCodeLine{2182   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KV\_BBL\_MIN"}, cs\%KV\_BBL\_min, \&}
\DoxyCodeLine{2183                  \textcolor{stringliteral}{"The minimum viscosities in the bottom boundary layer."}, \&}
\DoxyCodeLine{2184                  units=\textcolor{stringliteral}{"m2 s-\/1"}, default=kv\_background, scale=us\%m2\_s\_to\_Z2\_T)}
\DoxyCodeLine{2185   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KV\_TBL\_MIN"}, cs\%KV\_TBL\_min, \&}
\DoxyCodeLine{2186                  \textcolor{stringliteral}{"The minimum viscosities in the top boundary layer."}, \&}
\DoxyCodeLine{2187                  units=\textcolor{stringliteral}{"m2 s-\/1"}, default=kv\_background, scale=us\%m2\_s\_to\_Z2\_T)}
\DoxyCodeLine{2188   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"CORRECT\_BBL\_BOUNDS"}, cs\%correct\_BBL\_bounds, \&}
\DoxyCodeLine{2189                  \textcolor{stringliteral}{"If true, uses the correct bounds on the BBL thickness and "}//\&}
\DoxyCodeLine{2190                  \textcolor{stringliteral}{"viscosity so that the bottom layer feels the intended drag."}, \&}
\DoxyCodeLine{2191                  default=.false.)}
\DoxyCodeLine{2192 }
\DoxyCodeLine{2193   \textcolor{keywordflow}{if} (cs\%Channel\_drag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2194     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"SMAG\_LAP\_CONST"}, smag\_const1, default=-\/1.0)}
\DoxyCodeLine{2195 }
\DoxyCodeLine{2196     csmag\_chan\_dflt = 0.15}
\DoxyCodeLine{2197     \textcolor{keywordflow}{if} (smag\_const1 >= 0.0) csmag\_chan\_dflt = smag\_const1}
\DoxyCodeLine{2198 }
\DoxyCodeLine{2199     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"SMAG\_CONST\_CHANNEL"}, cs\%c\_Smag, \&}
\DoxyCodeLine{2200                  \textcolor{stringliteral}{"The nondimensional Laplacian Smagorinsky constant used "}//\&}
\DoxyCodeLine{2201                  \textcolor{stringliteral}{"in calculating the channel drag if it is enabled.  The "}//\&}
\DoxyCodeLine{2202                  \textcolor{stringliteral}{"default is to use the same value as SMAG\_LAP\_CONST if "}//\&}
\DoxyCodeLine{2203                  \textcolor{stringliteral}{"it is defined, or 0.15 if it is not. The value used is "}//\&}
\DoxyCodeLine{2204                  \textcolor{stringliteral}{"also 0.15 if the specified value is negative."}, \&}
\DoxyCodeLine{2205                  units=\textcolor{stringliteral}{"nondim"}, default=csmag\_chan\_dflt)}
\DoxyCodeLine{2206     \textcolor{keywordflow}{if} (cs\%c\_Smag < 0.0) cs\%c\_Smag = 0.15}
\DoxyCodeLine{2207 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2208 }
\DoxyCodeLine{2209   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MLE\_USE\_PBL\_MLD"}, mle\_use\_pbl\_mld, \&}
\DoxyCodeLine{2210                  default=.false., do\_not\_log=.true.)}
\DoxyCodeLine{2211 }
\DoxyCodeLine{2212   \textcolor{keywordflow}{if} (cs\%RiNo\_mix .and. kappa\_shear\_at\_vertex(param\_file)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2213     \textcolor{comment}{! This is necessary for reproduciblity across restarts in non-\/symmetric mode.}}
\DoxyCodeLine{2214     \textcolor{keyword}{call }pass\_var(visc\%Kv\_shear\_Bu, g\%Domain, position=corner, complete=.true.)}
\DoxyCodeLine{2215 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2216 }
\DoxyCodeLine{2217   \textcolor{keywordflow}{if} (cs\%bottomdraglaw) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2218     \textcolor{keyword}{allocate}(visc\%bbl\_thick\_u(isdb:iedb,jsd:jed)) ; visc\%bbl\_thick\_u(:,:) = 0.0}
\DoxyCodeLine{2219     \textcolor{keyword}{allocate}(visc\%kv\_bbl\_u(isdb:iedb,jsd:jed)) ; visc\%kv\_bbl\_u(:,:) = 0.0}
\DoxyCodeLine{2220     \textcolor{keyword}{allocate}(visc\%bbl\_thick\_v(isd:ied,jsdb:jedb)) ; visc\%bbl\_thick\_v(:,:) = 0.0}
\DoxyCodeLine{2221     \textcolor{keyword}{allocate}(visc\%kv\_bbl\_v(isd:ied,jsdb:jedb)) ; visc\%kv\_bbl\_v(:,:) = 0.0}
\DoxyCodeLine{2222     \textcolor{keyword}{allocate}(visc\%ustar\_bbl(isd:ied,jsd:jed)) ; visc\%ustar\_bbl(:,:) = 0.0}
\DoxyCodeLine{2223     \textcolor{keyword}{allocate}(visc\%TKE\_bbl(isd:ied,jsd:jed)) ; visc\%TKE\_bbl(:,:) = 0.0}
\DoxyCodeLine{2224 }
\DoxyCodeLine{2225     cs\%id\_bbl\_thick\_u = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'bbl\_thick\_u'}, \&}
\DoxyCodeLine{2226        diag\%axesCu1, time, \textcolor{stringliteral}{'BBL thickness at u points'}, \textcolor{stringliteral}{'m'}, conversion=us\%Z\_to\_m)}
\DoxyCodeLine{2227     cs\%id\_kv\_bbl\_u = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'kv\_bbl\_u'}, diag\%axesCu1, \&}
\DoxyCodeLine{2228        time, \textcolor{stringliteral}{'BBL viscosity at u points'}, \textcolor{stringliteral}{'m2 s-\/1'}, conversion=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{2229     cs\%id\_bbl\_u = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'bbl\_u'}, diag\%axesCu1, \&}
\DoxyCodeLine{2230        time, \textcolor{stringliteral}{'BBL mean u current'}, \textcolor{stringliteral}{'m s-\/1'}, conversion=us\%L\_T\_to\_m\_s)}
\DoxyCodeLine{2231     \textcolor{keywordflow}{if} (cs\%id\_bbl\_u>0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2232       \textcolor{keyword}{allocate}(cs\%bbl\_u(isdb:iedb,jsd:jed)) ; cs\%bbl\_u(:,:) = 0.0}
\DoxyCodeLine{2233 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2234     cs\%id\_bbl\_thick\_v = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'bbl\_thick\_v'}, \&}
\DoxyCodeLine{2235        diag\%axesCv1, time, \textcolor{stringliteral}{'BBL thickness at v points'}, \textcolor{stringliteral}{'m'}, conversion=us\%Z\_to\_m)}
\DoxyCodeLine{2236     cs\%id\_kv\_bbl\_v = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'kv\_bbl\_v'}, diag\%axesCv1, \&}
\DoxyCodeLine{2237        time, \textcolor{stringliteral}{'BBL viscosity at v points'}, \textcolor{stringliteral}{'m2 s-\/1'}, conversion=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{2238     cs\%id\_bbl\_v = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'bbl\_v'}, diag\%axesCv1, \&}
\DoxyCodeLine{2239        time, \textcolor{stringliteral}{'BBL mean v current'}, \textcolor{stringliteral}{'m s-\/1'}, conversion=us\%L\_T\_to\_m\_s)}
\DoxyCodeLine{2240     \textcolor{keywordflow}{if} (cs\%id\_bbl\_v>0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2241       \textcolor{keyword}{allocate}(cs\%bbl\_v(isd:ied,jsdb:jedb)) ; cs\%bbl\_v(:,:) = 0.0}
\DoxyCodeLine{2242 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2243     \textcolor{keywordflow}{if} (cs\%BBL\_use\_tidal\_bg) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2244       \textcolor{keyword}{allocate}(cs\%tideamp(isd:ied,jsd:jed)) ; cs\%tideamp(:,:) = 0.0}
\DoxyCodeLine{2245       filename = trim(cs\%inputdir) // trim(tideamp\_file)}
\DoxyCodeLine{2246       \textcolor{keyword}{call }log\_param(param\_file, mdl, \textcolor{stringliteral}{"INPUTDIR/TIDEAMP\_FILE"}, filename)}
\DoxyCodeLine{2247       \textcolor{keyword}{call }mom\_read\_data(filename, \textcolor{stringliteral}{'tideamp'}, cs\%tideamp, g\%domain, timelevel=1, scale=us\%m\_to\_Z*us\%T\_to\_s)}
\DoxyCodeLine{2248       \textcolor{keyword}{call }pass\_var(cs\%tideamp,g\%domain)}
\DoxyCodeLine{2249 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2250 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2251   \textcolor{keywordflow}{if} (cs\%Channel\_drag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2252     \textcolor{keyword}{allocate}(visc\%Ray\_u(isdb:iedb,jsd:jed,nz)) ; visc\%Ray\_u(:,:,:) = 0.0}
\DoxyCodeLine{2253     \textcolor{keyword}{allocate}(visc\%Ray\_v(isd:ied,jsdb:jedb,nz)) ; visc\%Ray\_v(:,:,:) = 0.0}
\DoxyCodeLine{2254     cs\%id\_Ray\_u = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Rayleigh\_u'}, diag\%axesCuL, \&}
\DoxyCodeLine{2255        time, \textcolor{stringliteral}{'Rayleigh drag velocity at u points'}, \textcolor{stringliteral}{'m s-\/1'}, conversion=us\%Z\_to\_m*us\%s\_to\_T)}
\DoxyCodeLine{2256     cs\%id\_Ray\_v = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Rayleigh\_v'}, diag\%axesCvL, \&}
\DoxyCodeLine{2257        time, \textcolor{stringliteral}{'Rayleigh drag velocity at v points'}, \textcolor{stringliteral}{'m s-\/1'}, conversion=us\%Z\_to\_m*us\%s\_to\_T)}
\DoxyCodeLine{2258 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2259 }
\DoxyCodeLine{2260 }
\DoxyCodeLine{2261   \textcolor{keywordflow}{if} (cs\%dynamic\_viscous\_ML) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2262     \textcolor{keyword}{allocate}(visc\%nkml\_visc\_u(isdb:iedb,jsd:jed)) ; visc\%nkml\_visc\_u(:,:) = 0.0}
\DoxyCodeLine{2263     \textcolor{keyword}{allocate}(visc\%nkml\_visc\_v(isd:ied,jsdb:jedb)) ; visc\%nkml\_visc\_v(:,:) = 0.0}
\DoxyCodeLine{2264     cs\%id\_nkml\_visc\_u = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'nkml\_visc\_u'}, \&}
\DoxyCodeLine{2265        diag\%axesCu1, time, \textcolor{stringliteral}{'Number of layers in viscous mixed layer at u points'}, \textcolor{stringliteral}{'m'})}
\DoxyCodeLine{2266     cs\%id\_nkml\_visc\_v = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'nkml\_visc\_v'}, \&}
\DoxyCodeLine{2267        diag\%axesCv1, time, \textcolor{stringliteral}{'Number of layers in viscous mixed layer at v points'}, \textcolor{stringliteral}{'m'})}
\DoxyCodeLine{2268 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2269 }
\DoxyCodeLine{2270   \textcolor{keyword}{call }register\_restart\_field\_as\_obsolete(\textcolor{stringliteral}{'Kd\_turb'},\textcolor{stringliteral}{'Kd\_shear'}, restart\_cs)}
\DoxyCodeLine{2271   \textcolor{keyword}{call }register\_restart\_field\_as\_obsolete(\textcolor{stringliteral}{'Kv\_turb'},\textcolor{stringliteral}{'Kv\_shear'}, restart\_cs)}
\DoxyCodeLine{2272 }
\DoxyCodeLine{2273   \textcolor{comment}{! Account for possible changes in dimensional scaling for variables that have been}}
\DoxyCodeLine{2274   \textcolor{comment}{! read from a restart file.}}
\DoxyCodeLine{2275   z\_rescale = 1.0}
\DoxyCodeLine{2276   \textcolor{keywordflow}{if} ((us\%m\_to\_Z\_restart /= 0.0) .and. (us\%m\_to\_Z\_restart /= us\%m\_to\_Z)) \&}
\DoxyCodeLine{2277     z\_rescale = us\%m\_to\_Z / us\%m\_to\_Z\_restart}
\DoxyCodeLine{2278   i\_t\_rescale = 1.0}
\DoxyCodeLine{2279   \textcolor{keywordflow}{if} ((us\%s\_to\_T\_restart /= 0.0) .and. (us\%s\_to\_T\_restart /= us\%s\_to\_T)) \&}
\DoxyCodeLine{2280     i\_t\_rescale = us\%s\_to\_T\_restart / us\%s\_to\_T}
\DoxyCodeLine{2281   z2\_t\_rescale = z\_rescale**2*i\_t\_rescale}
\DoxyCodeLine{2282 }
\DoxyCodeLine{2283   \textcolor{keywordflow}{if} (z2\_t\_rescale /= 1.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2284     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%Kd\_shear)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (query\_initialized(visc\%Kd\_shear, \textcolor{stringliteral}{"Kd\_shear"}, restart\_cs)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2285       \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2286         visc\%Kd\_shear(i,j,k) = z2\_t\_rescale * visc\%Kd\_shear(i,j,k)}
\DoxyCodeLine{2287 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2288 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{2289 }
\DoxyCodeLine{2290     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%Kv\_shear)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (query\_initialized(visc\%Kv\_shear, \textcolor{stringliteral}{"Kv\_shear"}, restart\_cs)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2291       \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2292         visc\%Kv\_shear(i,j,k) = z2\_t\_rescale * visc\%Kv\_shear(i,j,k)}
\DoxyCodeLine{2293 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2294 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{2295 }
\DoxyCodeLine{2296     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%Kv\_shear\_Bu)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (query\_initialized(visc\%Kv\_shear\_Bu, \textcolor{stringliteral}{"Kv\_shear\_Bu"}, restart\_cs)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2297       \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{2298         visc\%Kv\_shear\_Bu(i,j,k) = z2\_t\_rescale * visc\%Kv\_shear\_Bu(i,j,k)}
\DoxyCodeLine{2299 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2300 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{2301 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2302 }
\DoxyCodeLine{2303   \textcolor{keywordflow}{if} (mle\_use\_pbl\_mld .and. (z\_rescale /= 1.0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2304     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%MLD)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (query\_initialized(visc\%MLD, \textcolor{stringliteral}{"MLD"}, restart\_cs)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2305       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2306         visc\%MLD(i,j) = z\_rescale * visc\%MLD(i,j)}
\DoxyCodeLine{2307 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2308 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{2309 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2310 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__set__visc_ae2d9d9f74c1e9aec56257cfad372b0fd}\label{namespacemom__set__visc_ae2d9d9f74c1e9aec56257cfad372b0fd}} 
\index{mom\_set\_visc@{mom\_set\_visc}!set\_visc\_register\_restarts@{set\_visc\_register\_restarts}}
\index{set\_visc\_register\_restarts@{set\_visc\_register\_restarts}!mom\_set\_visc@{mom\_set\_visc}}
\doxysubsubsection{\texorpdfstring{set\_visc\_register\_restarts()}{set\_visc\_register\_restarts()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+set\+\_\+visc\+::set\+\_\+visc\+\_\+register\+\_\+restarts (\begin{DoxyParamCaption}\item[{type(hor\+\_\+index\+\_\+type), intent(in)}]{HI,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(param\+\_\+file\+\_\+type), intent(in)}]{param\+\_\+file,  }\item[{type(vertvisc\+\_\+type), intent(inout)}]{visc,  }\item[{type(mom\+\_\+restart\+\_\+cs), pointer}]{restart\+\_\+\+CS }\end{DoxyParamCaption})}



Register any fields associated with the vertvisc\+\_\+type. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em hi} & A horizontal index type structure. \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em param\+\_\+file} & A structure to parse for run-\/time parameters. \\
\hline
\mbox{\texttt{ in,out}}  & {\em visc} & A structure containing vertical viscosities and related fields. Allocated here. \\
\hline
 & {\em restart\+\_\+cs} & A pointer to the restart control structure. \\
\hline
\end{DoxyParams}


Definition at line 1886 of file M\+O\+M\+\_\+set\+\_\+viscosity.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1887   \textcolor{keywordtype}{type}(hor\_index\_type),    \textcolor{keywordtype}{intent(in)}    :: HI\textcolor{comment}{         !< A horizontal index type structure.}}
\DoxyCodeLine{1888   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{         !< The ocean's vertical grid structure.}}
\DoxyCodeLine{1889   \textcolor{keywordtype}{type}(param\_file\_type),   \textcolor{keywordtype}{intent(in)}    :: param\_file\textcolor{comment}{ !< A structure to parse for run-\/time}}
\DoxyCodeLine{1890 \textcolor{comment}{                                                       !! parameters.}}
\DoxyCodeLine{1891   \textcolor{keywordtype}{type}(vertvisc\_type),     \textcolor{keywordtype}{intent(inout)} :: visc\textcolor{comment}{       !< A structure containing vertical}}
\DoxyCodeLine{1892 \textcolor{comment}{                                                       !! viscosities and related fields.}}
\DoxyCodeLine{1893 \textcolor{comment}{                                                       !! Allocated here.}}
\DoxyCodeLine{1894   \textcolor{keywordtype}{type}(MOM\_restart\_CS),    \textcolor{keywordtype}{pointer}       :: restart\_CS\textcolor{comment}{ !< A pointer to the restart control structure.}}
\DoxyCodeLine{1895   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{1896   \textcolor{keywordtype}{logical} :: use\_kappa\_shear, KS\_at\_vertex}
\DoxyCodeLine{1897   \textcolor{keywordtype}{logical} :: adiabatic, useKPP, useEPBL}
\DoxyCodeLine{1898   \textcolor{keywordtype}{logical} :: use\_CVMix\_shear, MLE\_use\_PBL\_MLD, use\_CVMix\_conv}
\DoxyCodeLine{1899   \textcolor{keywordtype}{integer} :: isd, ied, jsd, jed, nz}
\DoxyCodeLine{1900 \textcolor{keywordtype}{  real} :: hfreeze\textcolor{comment}{ !< If hfreeze > 0 [m], melt potential will be computed.}}
\DoxyCodeLine{1901   \textcolor{keywordtype}{character(len=40)}  :: mdl = \textcolor{stringliteral}{"MOM\_set\_visc"}  \textcolor{comment}{! This module's name.}}
\DoxyCodeLine{1902   isd = hi\%isd ; ied = hi\%ied ; jsd = hi\%jsd ; jed = hi\%jed ; nz = gv\%ke}
\DoxyCodeLine{1903 }
\DoxyCodeLine{1904   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ADIABATIC"}, adiabatic, default=.false., \&}
\DoxyCodeLine{1905                  do\_not\_log=.true.)}
\DoxyCodeLine{1906 }
\DoxyCodeLine{1907   use\_kappa\_shear = .false. ; ks\_at\_vertex = .false. ; use\_cvmix\_shear = .false.}
\DoxyCodeLine{1908   usekpp = .false. ; useepbl = .false. ; use\_cvmix\_conv = .false.}
\DoxyCodeLine{1909 }
\DoxyCodeLine{1910   \textcolor{keywordflow}{if} (.not.adiabatic) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1911     use\_kappa\_shear = kappa\_shear\_is\_used(param\_file)}
\DoxyCodeLine{1912     ks\_at\_vertex    = kappa\_shear\_at\_vertex(param\_file)}
\DoxyCodeLine{1913     use\_cvmix\_shear = cvmix\_shear\_is\_used(param\_file)}
\DoxyCodeLine{1914     use\_cvmix\_conv  = cvmix\_conv\_is\_used(param\_file)}
\DoxyCodeLine{1915     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"USE\_KPP"}, usekpp, \&}
\DoxyCodeLine{1916                  \textcolor{stringliteral}{"If true, turns on the [CVMix] KPP scheme of Large et al., 1994, "}//\&}
\DoxyCodeLine{1917                  \textcolor{stringliteral}{"to calculate diffusivities and non-\/local transport in the OBL."}, \&}
\DoxyCodeLine{1918                  default=.false., do\_not\_log=.true.)}
\DoxyCodeLine{1919     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ENERGETICS\_SFC\_PBL"}, useepbl, \&}
\DoxyCodeLine{1920                  \textcolor{stringliteral}{"If true, use an implied energetics planetary boundary "}//\&}
\DoxyCodeLine{1921                  \textcolor{stringliteral}{"layer scheme to determine the diffusivity and viscosity "}//\&}
\DoxyCodeLine{1922                  \textcolor{stringliteral}{"in the surface boundary layer."}, default=.false., do\_not\_log=.true.)}
\DoxyCodeLine{1923 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1924 }
\DoxyCodeLine{1925   \textcolor{keywordflow}{if} (use\_kappa\_shear .or. usekpp .or. useepbl .or. use\_cvmix\_shear .or. use\_cvmix\_conv) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1926     \textcolor{keyword}{call }safe\_alloc\_ptr(visc\%Kd\_shear, isd, ied, jsd, jed, nz+1)}
\DoxyCodeLine{1927     \textcolor{keyword}{call }register\_restart\_field(visc\%Kd\_shear, \textcolor{stringliteral}{"Kd\_shear"}, .false., restart\_cs, \&}
\DoxyCodeLine{1928                   \textcolor{stringliteral}{"Shear-\/driven turbulent diffusivity at interfaces"}, \textcolor{stringliteral}{"m2 s-\/1"}, z\_grid=\textcolor{stringliteral}{'i'})}
\DoxyCodeLine{1929 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1930   \textcolor{keywordflow}{if} (usekpp .or. useepbl .or. use\_cvmix\_shear .or. use\_cvmix\_conv .or. \&}
\DoxyCodeLine{1931       (use\_kappa\_shear .and. .not.ks\_at\_vertex )) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1932     \textcolor{keyword}{call }safe\_alloc\_ptr(visc\%Kv\_shear, isd, ied, jsd, jed, nz+1)}
\DoxyCodeLine{1933     \textcolor{keyword}{call }register\_restart\_field(visc\%Kv\_shear, \textcolor{stringliteral}{"Kv\_shear"}, .false., restart\_cs, \&}
\DoxyCodeLine{1934                   \textcolor{stringliteral}{"Shear-\/driven turbulent viscosity at interfaces"}, \textcolor{stringliteral}{"m2 s-\/1"}, z\_grid=\textcolor{stringliteral}{'i'})}
\DoxyCodeLine{1935 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1936   \textcolor{keywordflow}{if} (use\_kappa\_shear .and. ks\_at\_vertex) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1937     \textcolor{keyword}{call }safe\_alloc\_ptr(visc\%TKE\_turb, hi\%IsdB, hi\%IedB, hi\%JsdB, hi\%JedB, nz+1)}
\DoxyCodeLine{1938     \textcolor{keyword}{call }safe\_alloc\_ptr(visc\%Kv\_shear\_Bu, hi\%IsdB, hi\%IedB, hi\%JsdB, hi\%JedB, nz+1)}
\DoxyCodeLine{1939     \textcolor{keyword}{call }register\_restart\_field(visc\%Kv\_shear\_Bu, \textcolor{stringliteral}{"Kv\_shear\_Bu"}, .false., restart\_cs, \&}
\DoxyCodeLine{1940                   \textcolor{stringliteral}{"Shear-\/driven turbulent viscosity at vertex interfaces"}, \textcolor{stringliteral}{"m2 s-\/1"}, \&}
\DoxyCodeLine{1941                   hor\_grid=\textcolor{stringliteral}{"Bu"}, z\_grid=\textcolor{stringliteral}{'i'})}
\DoxyCodeLine{1942   \textcolor{keywordflow}{elseif} (use\_kappa\_shear) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1943     \textcolor{keyword}{call }safe\_alloc\_ptr(visc\%TKE\_turb, isd, ied, jsd, jed, nz+1)}
\DoxyCodeLine{1944 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1945 }
\DoxyCodeLine{1946   \textcolor{keywordflow}{if} (usekpp) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1947     \textcolor{comment}{! MOM\_bkgnd\_mixing uses Kv\_slow when KPP is defined.}}
\DoxyCodeLine{1948     \textcolor{keyword}{call }safe\_alloc\_ptr(visc\%Kv\_slow, isd, ied, jsd, jed, nz+1)}
\DoxyCodeLine{1949 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1950 }
\DoxyCodeLine{1951   \textcolor{comment}{! visc\%MLD is used to communicate the state of the (e)PBL or KPP to the rest of the model}}
\DoxyCodeLine{1952   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MLE\_USE\_PBL\_MLD"}, mle\_use\_pbl\_mld, \&}
\DoxyCodeLine{1953                  default=.false., do\_not\_log=.true.)}
\DoxyCodeLine{1954   \textcolor{comment}{! visc\%MLD needs to be allocated when melt potential is computed (HFREEZE>0)}}
\DoxyCodeLine{1955   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"HFREEZE"}, hfreeze, \&}
\DoxyCodeLine{1956                  default=-\/1.0, do\_not\_log=.true.)}
\DoxyCodeLine{1957 }
\DoxyCodeLine{1958   \textcolor{keywordflow}{if} (mle\_use\_pbl\_mld) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1959     \textcolor{keyword}{call }safe\_alloc\_ptr(visc\%MLD, isd, ied, jsd, jed)}
\DoxyCodeLine{1960     \textcolor{keyword}{call }register\_restart\_field(visc\%MLD, \textcolor{stringliteral}{"MLD"}, .false., restart\_cs, \&}
\DoxyCodeLine{1961                   \textcolor{stringliteral}{"Instantaneous active mixing layer depth"}, \textcolor{stringliteral}{"m"})}
\DoxyCodeLine{1962 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1963 }
\DoxyCodeLine{1964   \textcolor{keywordflow}{if} (hfreeze >= 0.0 .and. .not.mle\_use\_pbl\_mld) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1965     \textcolor{keyword}{call }safe\_alloc\_ptr(visc\%MLD, isd, ied, jsd, jed)}
\DoxyCodeLine{1966 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1967 }
\DoxyCodeLine{1968 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__set__visc_a9865113fe07928e7a240c2868ed45e5f}\label{namespacemom__set__visc_a9865113fe07928e7a240c2868ed45e5f}} 
\index{mom\_set\_visc@{mom\_set\_visc}!set\_viscous\_bbl@{set\_viscous\_bbl}}
\index{set\_viscous\_bbl@{set\_viscous\_bbl}!mom\_set\_visc@{mom\_set\_visc}}
\doxysubsubsection{\texorpdfstring{set\_viscous\_bbl()}{set\_viscous\_bbl()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+set\+\_\+visc\+::set\+\_\+viscous\+\_\+bbl (\begin{DoxyParamCaption}\item[{real, dimension(szib\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{u,  }\item[{real, dimension(szi\+\_\+(g),szjb\+\_\+(g),szk\+\_\+(g)), intent(in)}]{v,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{h,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(in)}]{tv,  }\item[{type(vertvisc\+\_\+type), intent(inout)}]{visc,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(inout)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__set__visc_1_1set__visc__cs}{set\+\_\+visc\+\_\+cs}}), pointer}]{CS,  }\item[{logical, intent(in), optional}]{symmetrize }\end{DoxyParamCaption})}



Calculates the thickness of the bottom boundary layer and the viscosity within that layer. 

A drag law is used, either linearized about an assumed bottom velocity or using the actual near-\/bottom velocities combined with an assumed unresolved velocity. The bottom boundary layer thickness is limited by a combination of stratification and rotation, as in the paper of Killworth and Edwards, J\+PO 1999. It is not necessary to calculate the thickness and viscosity every time step; instead previous values may be used.\hypertarget{namespacemom__set__visc_set_viscous_BBL}{}\doxysubsection{Viscous Bottom Boundary Layer}\label{namespacemom__set__visc_set_viscous_BBL}
If \mbox{\hyperlink{structmom__set__visc_1_1set__visc__cs_aca87adb9636e724f7ccb78e7c640f399}{set\+\_\+visc\+\_\+cs.\+bottomdraglaw}} is True then a bottom boundary layer viscosity and thickness are calculated so that the bottom stress is \[ \mathbf{\tau}_b = C_d | U_{bbl} | \mathbf{u}_{bbl} \] If \mbox{\hyperlink{structmom__set__visc_1_1set__visc__cs_aca87adb9636e724f7ccb78e7c640f399}{set\+\_\+visc\+\_\+cs.\+bottomdraglaw}} is True then the term $|U_{bbl}|$ is set equal to the value in \mbox{\hyperlink{structmom__set__visc_1_1set__visc__cs_a8e5f72291c7e5c5e602c9b4765d63ae0}{set\+\_\+visc\+\_\+cs.\+drag\+\_\+bg\+\_\+vel}} so that $C_d |U_{bbl}|$ becomes a Rayleigh bottom drag. Otherwise $|U_{bbl}|$ is found by averaging the flow over the bottom \mbox{\hyperlink{structmom__set__visc_1_1set__visc__cs_a20768c8872b3eab25e59dfe7c5ff62a0}{set\+\_\+visc\+\_\+cs.\+hbbl}} of the model, adding the amplitude of tides \mbox{\hyperlink{structmom__set__visc_1_1set__visc__cs_a765c0eec288ee86e83afc501b757a821}{set\+\_\+visc\+\_\+cs.\+tideamp}} and a constant \mbox{\hyperlink{structmom__set__visc_1_1set__visc__cs_a8e5f72291c7e5c5e602c9b4765d63ae0}{set\+\_\+visc\+\_\+cs.\+drag\+\_\+bg\+\_\+vel}}. For these calculations the vertical grid at the velocity component locations is found by \[ \begin{array}{ll} \frac{2 h^- h^+}{h^- + h^+} & u \left( h^+ - h^-\right) >= 0 \\ \frac{1}{2} \left( h^- + h^+ \right) & u \left( h^+ - h^-\right) < 0 \end{array} \] which biases towards the thin cell if the thin cell is upwind. Biasing the grid toward thin upwind cells helps increase the effect of viscosity and inhibits flow out of these thin cells.

After diagnosing $|U_{bbl}|$ over a fixed depth an active viscous boundary layer thickness is found using the ideas of Killworth and Edwards, 1999 (hereafter K\+W99). K\+W99 solve the equation \[ \left( \frac{h_{bbl}}{h_f} \right)^2 + \frac{h_{bbl}}{h_N} = 1 \] for the boundary layer depth $h_{bbl}$. Here \[ h_f = \frac{C_n u_*}{f} \] is the rotation controlled boundary layer depth in the absence of stratification. $u_*$ is the surface friction speed given by \[ u_*^2 = C_d |U_{bbl}|^2 \] and is a function of near bottom model flow. \[ h_N = \frac{C_i u_*}{N} = \frac{ (C_i u_* )^2 }{g^\prime} \] is the stratification controlled boundary layer depth. The non-\/dimensional parameters $C_n=0.5$ and $C_i=20$ are suggested by Zilitinkevich and Mironov, 1996.

If a Richardson number dependent mixing scheme is being used, as indicated by \mbox{\hyperlink{structmom__set__visc_1_1set__visc__cs_abf6d0c52b4c7697848e11790fed715dc}{set\+\_\+visc\+\_\+cs.\+rino\+\_\+mix}}, then the boundary layer thickness is bounded to be no larger than a half of \mbox{\hyperlink{structmom__set__visc_1_1set__visc__cs_a20768c8872b3eab25e59dfe7c5ff62a0}{set\+\_\+visc\+\_\+cs.\+hbbl}} .

\begin{DoxyRefDesc}{Todo}
\item[\mbox{\hyperlink{todo__todo000007}{Todo}}]Channel drag needs to be explained\end{DoxyRefDesc}


A B\+BL viscosity is calculated so that the no-\/slip boundary condition in the vertical viscosity solver implies the stress $\mathbf{\tau}_b$.\hypertarget{namespacemom__set__visc_set_viscous_BBL_ref}{}\doxysubsubsection{References}\label{namespacemom__set__visc_set_viscous_BBL_ref}
\begin{DoxyItemize}
\item Killworth, P. D., and N. R. Edwards, 1999\+: A Turbulent Bottom Boundary Layer Code for Use in Numerical Ocean Models. J. Phys. Oceanogr., 29, 1221-\/1238, \href{https://doi.org/10.1175/1520-0485(1999)029<1221:ATBBLC>2.0.CO;2}{\texttt{ doi\+:10.\+1175/1520-\/0485(1999)029$<$1221\+:A\+T\+B\+B\+LC$>$2.\+0.\+CO;2}} \item Zilitinkevich, S., Mironov, D.\+V., 1996\+: A multi-\/limit formulation for the equilibrium depth of a stably stratified boundary layer. Boundary-\/\+Layer Meteorology 81, 325-\/351. \href{https://doi.org/10.1007/BF02430334}{\texttt{ doi\+:10.\+1007/\+B\+F02430334}} 
\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in,out}}  & {\em g} & The ocean\textquotesingle{}s grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in}}  & {\em u} & The zonal velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em v} & The meridional velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em tv} & A structure containing pointers to any available thermodynamic fields. Absent fields have N\+U\+LL ptrs.. \\
\hline
\mbox{\texttt{ in,out}}  & {\em visc} & A structure containing vertical viscosities and related fields. \\
\hline
 & {\em cs} & The control structure returned by a previous call to set\+\_\+visc\+\_\+init. \\
\hline
\mbox{\texttt{ in}}  & {\em symmetrize} & If present and true, do extra calculations of those values in visc that would be calculated with symmetric memory. \\
\hline
\end{DoxyParams}
\end{DoxyItemize}


Definition at line 192 of file M\+O\+M\+\_\+set\+\_\+viscosity.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{193   \textcolor{keywordtype}{type}(ocean\_grid\_type),    \textcolor{keywordtype}{intent(inout)} :: G\textcolor{comment}{    !< The ocean's grid structure.}}
\DoxyCodeLine{194   \textcolor{keywordtype}{type}(verticalGrid\_type),  \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure.}}
\DoxyCodeLine{195   \textcolor{keywordtype}{type}(unit\_scale\_type),    \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{   !< A dimensional unit scaling type}}
\DoxyCodeLine{196 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{197                             \textcolor{keywordtype}{intent(in)}    :: u\textcolor{comment}{    !< The zonal velocity [L T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{198 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{199                             \textcolor{keywordtype}{intent(in)}    :: v\textcolor{comment}{    !< The meridional velocity [L T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{200 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))},  \&}
\DoxyCodeLine{201                             \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{    !< Layer thicknesses [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{202   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),    \textcolor{keywordtype}{intent(in)}    :: tv\textcolor{comment}{   !< A structure containing pointers to any}}
\DoxyCodeLine{203 \textcolor{comment}{                                                  !! available thermodynamic fields. Absent fields}}
\DoxyCodeLine{204 \textcolor{comment}{                                                  !! have NULL ptrs..}}
\DoxyCodeLine{205   \textcolor{keywordtype}{type}(vertvisc\_type),      \textcolor{keywordtype}{intent(inout)} :: visc\textcolor{comment}{ !< A structure containing vertical viscosities and}}
\DoxyCodeLine{206 \textcolor{comment}{                                                  !! related fields.}}
\DoxyCodeLine{207   \textcolor{keywordtype}{type}(set\_visc\_CS),        \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{   !< The control structure returned by a previous}}
\DoxyCodeLine{208 \textcolor{comment}{                                                  !! call to set\_visc\_init.}}
\DoxyCodeLine{209   \textcolor{keywordtype}{logical},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: symmetrize\textcolor{comment}{ !< If present and true, do extra calculations}}
\DoxyCodeLine{210 \textcolor{comment}{                                                  !! of those values in visc that would be}}
\DoxyCodeLine{211 \textcolor{comment}{                                                  !! calculated with symmetric memory.}}
\DoxyCodeLine{212 }
\DoxyCodeLine{213   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{214 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G))} :: \&}
\DoxyCodeLine{215     ustar, \&    \textcolor{comment}{!   The bottom friction velocity [Z T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{216     T\_EOS, \&    \textcolor{comment}{!   The temperature used to calculate the partial derivatives}}
\DoxyCodeLine{217                 \textcolor{comment}{! of density with T and S [degC].}}
\DoxyCodeLine{218     s\_eos, \&    \textcolor{comment}{!   The salinity used to calculate the partial derivatives}}
\DoxyCodeLine{219                 \textcolor{comment}{! of density with T and S [ppt].}}
\DoxyCodeLine{220     dr\_dt, \&    \textcolor{comment}{!   Partial derivative of the density in the bottom boundary}}
\DoxyCodeLine{221                 \textcolor{comment}{! layer with temperature [R degC-\/1 \string~> kg m-\/3 degC-\/1].}}
\DoxyCodeLine{222     dr\_ds, \&    \textcolor{comment}{!   Partial derivative of the density in the bottom boundary}}
\DoxyCodeLine{223                 \textcolor{comment}{! layer with salinity [R ppt-\/1 \string~> kg m-\/3 ppt-\/1].}}
\DoxyCodeLine{224     press       \textcolor{comment}{!   The pressure at which dR\_dT and dR\_dS are evaluated [R L2 T-\/2 \string~> Pa].}}
\DoxyCodeLine{225 \textcolor{keywordtype}{  real} :: htot      \textcolor{comment}{! Sum of the layer thicknesses up to some point [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{226 \textcolor{keywordtype}{  real} :: htot\_vel  \textcolor{comment}{! Sum of the layer thicknesses up to some point [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{227 }
\DoxyCodeLine{228 \textcolor{keywordtype}{  real} :: Rhtot \textcolor{comment}{! Running sum of thicknesses times the layer potential}}
\DoxyCodeLine{229                 \textcolor{comment}{! densities [H R \string~> kg m-\/2 or kg2 m-\/5].}}
\DoxyCodeLine{230 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G))} :: \&}
\DoxyCodeLine{231     D\_u, \&      \textcolor{comment}{! Bottom depth interpolated to u points [Z \string~> m].}}
\DoxyCodeLine{232     mask\_u      \textcolor{comment}{! A mask that disables any contributions from u points that}}
\DoxyCodeLine{233                 \textcolor{comment}{! are land or past open boundary conditions [nondim], 0 or 1.}}
\DoxyCodeLine{234 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G))} :: \&}
\DoxyCodeLine{235     D\_v, \&      \textcolor{comment}{! Bottom depth interpolated to v points [Z \string~> m].}}
\DoxyCodeLine{236     mask\_v      \textcolor{comment}{! A mask that disables any contributions from v points that}}
\DoxyCodeLine{237                 \textcolor{comment}{! are land or past open boundary conditions [nondim], 0 or 1.}}
\DoxyCodeLine{238 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZK\_(G))} :: \&}
\DoxyCodeLine{239     h\_at\_vel, \& \textcolor{comment}{! Layer thickness at a velocity point, using an upwind-\/biased}}
\DoxyCodeLine{240                 \textcolor{comment}{! second order accurate estimate based on the previous velocity}}
\DoxyCodeLine{241                 \textcolor{comment}{! direction [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{242     h\_vel, \&    \textcolor{comment}{! Arithmetic mean of the layer thicknesses adjacent to a}}
\DoxyCodeLine{243                 \textcolor{comment}{! velocity point [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{244     t\_vel, \&    \textcolor{comment}{! Arithmetic mean of the layer temperatures adjacent to a}}
\DoxyCodeLine{245                 \textcolor{comment}{! velocity point [degC].}}
\DoxyCodeLine{246     s\_vel, \&    \textcolor{comment}{! Arithmetic mean of the layer salinities adjacent to a}}
\DoxyCodeLine{247                 \textcolor{comment}{! velocity point [ppt].}}
\DoxyCodeLine{248     rml\_vel     \textcolor{comment}{! Arithmetic mean of the layer coordinate densities adjacent}}
\DoxyCodeLine{249                 \textcolor{comment}{! to a velocity point [R \string~> kg m-\/3].}}
\DoxyCodeLine{250 }
\DoxyCodeLine{251 \textcolor{keywordtype}{  real} :: h\_vel\_pos        \textcolor{comment}{! The arithmetic mean thickness at a velocity point}}
\DoxyCodeLine{252                            \textcolor{comment}{! plus H\_neglect to avoid 0 values [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{253 \textcolor{keywordtype}{  real} :: ustarsq          \textcolor{comment}{! 400 times the square of ustar, times}}
\DoxyCodeLine{254                            \textcolor{comment}{! Rho0 divided by G\_Earth and the conversion}}
\DoxyCodeLine{255                            \textcolor{comment}{! from m to thickness units [H R \string~> kg m-\/2 or kg2 m-\/5].}}
\DoxyCodeLine{256 \textcolor{keywordtype}{  real} :: cdrag\_sqrt\_Z     \textcolor{comment}{! Square root of the drag coefficient, times a unit conversion}}
\DoxyCodeLine{257                            \textcolor{comment}{! factor from lateral lengths to vertical depths [Z L-\/1 \string~> 1].}}
\DoxyCodeLine{258 \textcolor{keywordtype}{  real} :: cdrag\_sqrt       \textcolor{comment}{! Square root of the drag coefficient [nondim].}}
\DoxyCodeLine{259 \textcolor{keywordtype}{  real} :: oldfn            \textcolor{comment}{! The integrated energy required to}}
\DoxyCodeLine{260                            \textcolor{comment}{! entrain up to the bottom of the layer,}}
\DoxyCodeLine{261                            \textcolor{comment}{! divided by G\_Earth [H R \string~> kg m-\/2 or kg2 m-\/5].}}
\DoxyCodeLine{262 \textcolor{keywordtype}{  real} :: Dfn              \textcolor{comment}{! The increment in oldfn for entraining}}
\DoxyCodeLine{263                            \textcolor{comment}{! the layer [H R \string~> kg m-\/2 or kg2 m-\/5].}}
\DoxyCodeLine{264 \textcolor{keywordtype}{  real} :: Dh               \textcolor{comment}{! The increment in layer thickness from}}
\DoxyCodeLine{265                            \textcolor{comment}{! the present layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{266 \textcolor{keywordtype}{  real} :: bbl\_thick        \textcolor{comment}{! The thickness of the bottom boundary layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{267 \textcolor{keywordtype}{  real} :: bbl\_thick\_Z      \textcolor{comment}{! The thickness of the bottom boundary layer [Z \string~> m].}}
\DoxyCodeLine{268 \textcolor{keywordtype}{  real} :: kv\_bbl           \textcolor{comment}{! The bottom boundary layer viscosity [Z2 T-\/1 \string~> m2 s-\/1].}}
\DoxyCodeLine{269 \textcolor{keywordtype}{  real} :: C2f              \textcolor{comment}{! C2f = 2*f at velocity points [T-\/1 \string~> s-\/1].}}
\DoxyCodeLine{270 }
\DoxyCodeLine{271 \textcolor{keywordtype}{  real} :: U\_bg\_sq          \textcolor{comment}{! The square of an assumed background}}
\DoxyCodeLine{272                            \textcolor{comment}{! velocity, for calculating the mean}}
\DoxyCodeLine{273                            \textcolor{comment}{! magnitude near the bottom for use in the}}
\DoxyCodeLine{274                            \textcolor{comment}{! quadratic bottom drag [L2 T-\/2 \string~> m2 s-\/2].}}
\DoxyCodeLine{275 \textcolor{keywordtype}{  real} :: hwtot            \textcolor{comment}{! Sum of the thicknesses used to calculate}}
\DoxyCodeLine{276                            \textcolor{comment}{! the near-\/bottom velocity magnitude [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{277 \textcolor{keywordtype}{  real} :: hutot            \textcolor{comment}{! Running sum of thicknesses times the}}
\DoxyCodeLine{278                            \textcolor{comment}{! velocity magnitudes [H T T-\/1 \string~> m2 s-\/1 or kg m-\/1 s-\/1].}}
\DoxyCodeLine{279 \textcolor{keywordtype}{  real} :: Thtot            \textcolor{comment}{! Running sum of thickness times temperature [degC H \string~> degC m or degC kg m-\/2].}}
\DoxyCodeLine{280 \textcolor{keywordtype}{  real} :: Shtot            \textcolor{comment}{! Running sum of thickness times salinity [ppt H \string~> ppt m or ppt kg m-\/2].}}
\DoxyCodeLine{281 \textcolor{keywordtype}{  real} :: hweight          \textcolor{comment}{! The thickness of a layer that is within Hbbl}}
\DoxyCodeLine{282                            \textcolor{comment}{! of the bottom [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{283 \textcolor{keywordtype}{  real} :: v\_at\_u, u\_at\_v   \textcolor{comment}{! v at a u point or vice versa [L T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{284 \textcolor{keywordtype}{  real} :: Rho0x400\_G       \textcolor{comment}{! 400*Rho0/G\_Earth, times unit conversion factors}}
\DoxyCodeLine{285                            \textcolor{comment}{! [R T2 H Z-\/2 \string~> kg s2 m-\/4 or kg2 s2 m-\/7].}}
\DoxyCodeLine{286                            \textcolor{comment}{! The 400 is a constant proposed by Killworth and Edwards, 1999.}}
\DoxyCodeLine{287 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),max(GV\%nk\_rho\_varies,1))} :: \&}
\DoxyCodeLine{288     Rml                    \textcolor{comment}{! The mixed layer coordinate density [R \string~> kg m-\/3].}}
\DoxyCodeLine{289 \textcolor{keywordtype}{  real} :: p\_ref(SZI\_(G))   \textcolor{comment}{!   The pressure used to calculate the coordinate}}
\DoxyCodeLine{290                            \textcolor{comment}{! density [R L2 T-\/2 \string~> Pa] (usually set to 2e7 Pa = 2000 dbar).}}
\DoxyCodeLine{291 }
\DoxyCodeLine{292 \textcolor{keywordtype}{  real} :: D\_vel            \textcolor{comment}{! The bottom depth at a velocity point [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{293 \textcolor{keywordtype}{  real} :: Dp, Dm           \textcolor{comment}{! The depths at the edges of a velocity cell [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{294 \textcolor{keywordtype}{  real} :: a                \textcolor{comment}{! a is the curvature of the bottom depth across a}}
\DoxyCodeLine{295                            \textcolor{comment}{! cell, times the cell width squared [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{296 \textcolor{keywordtype}{  real} :: a\_3, a\_12        \textcolor{comment}{! a/3 and a/12 [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{297 \textcolor{keywordtype}{  real} :: C24\_a            \textcolor{comment}{! 24/a [H-\/1 \string~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{298 \textcolor{keywordtype}{  real} :: slope            \textcolor{comment}{! The absolute value of the bottom depth slope across}}
\DoxyCodeLine{299                            \textcolor{comment}{! a cell times the cell width [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{300 \textcolor{keywordtype}{  real} :: apb\_4a, ax2\_3apb \textcolor{comment}{! Various nondimensional ratios of a and slope.}}
\DoxyCodeLine{301 \textcolor{keywordtype}{  real} :: a2x48\_apb3, Iapb, Ibma\_2 \textcolor{comment}{! Combinations of a and slope [H-\/1 \string~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{302   \textcolor{comment}{! All of the following "volumes" have units of thickness because they are normalized}}
\DoxyCodeLine{303   \textcolor{comment}{! by the full horizontal area of a velocity cell.}}
\DoxyCodeLine{304 \textcolor{keywordtype}{  real} :: Vol\_open         \textcolor{comment}{! The cell volume above which it is open [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{305 \textcolor{keywordtype}{  real} :: Vol\_direct       \textcolor{comment}{! With less than Vol\_direct [H \string~> m or kg m-\/2], there is a direct}}
\DoxyCodeLine{306                            \textcolor{comment}{! solution of a cubic equation for L.}}
\DoxyCodeLine{307 \textcolor{keywordtype}{  real} :: Vol\_2\_reg        \textcolor{comment}{! The cell volume above which there are two separate}}
\DoxyCodeLine{308                            \textcolor{comment}{! open areas that must be integrated [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{309 \textcolor{keywordtype}{  real} :: vol              \textcolor{comment}{! The volume below the interface whose normalized}}
\DoxyCodeLine{310                            \textcolor{comment}{! width is being sought [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{311 \textcolor{keywordtype}{  real} :: vol\_below        \textcolor{comment}{! The volume below the interface below the one that}}
\DoxyCodeLine{312                            \textcolor{comment}{! is currently under consideration [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{313 \textcolor{keywordtype}{  real} :: Vol\_err          \textcolor{comment}{! The error in the volume with the latest estimate of}}
\DoxyCodeLine{314                            \textcolor{comment}{! L, or the error for the interface below [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{315 \textcolor{keywordtype}{  real} :: Vol\_quit         \textcolor{comment}{! The volume error below which to quit iterating [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{316 \textcolor{keywordtype}{  real} :: Vol\_tol          \textcolor{comment}{! A volume error tolerance [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{317 \textcolor{keywordtype}{  real} :: L(SZK\_(G)+1)     \textcolor{comment}{! The fraction of the full cell width that is open at}}
\DoxyCodeLine{318                            \textcolor{comment}{! the depth of each interface [nondim].}}
\DoxyCodeLine{319 \textcolor{keywordtype}{  real} :: L\_direct         \textcolor{comment}{! The value of L above volume Vol\_direct [nondim].}}
\DoxyCodeLine{320 \textcolor{keywordtype}{  real} :: L\_max, L\_min     \textcolor{comment}{! Upper and lower bounds on the correct value for L  [nondim].}}
\DoxyCodeLine{321 \textcolor{keywordtype}{  real} :: Vol\_err\_max      \textcolor{comment}{! The volume errors for the upper and lower bounds on}}
\DoxyCodeLine{322 \textcolor{keywordtype}{  real} :: Vol\_err\_min      \textcolor{comment}{! the correct value for L [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{323 \textcolor{keywordtype}{  real} :: Vol\_0            \textcolor{comment}{! A deeper volume with known width L0 [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{324 \textcolor{keywordtype}{  real} :: L0               \textcolor{comment}{! The value of L above volume Vol\_0 [nondim].}}
\DoxyCodeLine{325 \textcolor{keywordtype}{  real} :: dVol             \textcolor{comment}{! vol -\/ Vol\_0 [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{326 \textcolor{keywordtype}{  real} :: dV\_dL2           \textcolor{comment}{! The partial derivative of volume with L squared}}
\DoxyCodeLine{327                            \textcolor{comment}{! evaluated at L=L0 [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{328 \textcolor{keywordtype}{  real} :: h\_neglect        \textcolor{comment}{! A thickness that is so small it is usually lost}}
\DoxyCodeLine{329                            \textcolor{comment}{! in roundoff and can be neglected [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{330 \textcolor{keywordtype}{  real} :: ustH             \textcolor{comment}{! ustar converted to units of H T-\/1 [H T-\/1 \string~> m s-\/1 or kg m-\/2 s-\/1].}}
\DoxyCodeLine{331 \textcolor{keywordtype}{  real} :: root             \textcolor{comment}{! A temporary variable [H T-\/1 \string~> m s-\/1 or kg m-\/2 s-\/1].}}
\DoxyCodeLine{332 }
\DoxyCodeLine{333 \textcolor{keywordtype}{  real} :: Cell\_width       \textcolor{comment}{! The transverse width of the velocity cell [L \string~> m].}}
\DoxyCodeLine{334 \textcolor{keywordtype}{  real} :: Rayleigh         \textcolor{comment}{! A nondimensional value that is multiplied by the layer's}}
\DoxyCodeLine{335                            \textcolor{comment}{! velocity magnitude to give the Rayleigh drag velocity, times}}
\DoxyCodeLine{336                            \textcolor{comment}{! a lateral to vertical distance conversion factor [Z L-\/1 \string~> 1].}}
\DoxyCodeLine{337 \textcolor{keywordtype}{  real} :: gam              \textcolor{comment}{! The ratio of the change in the open interface width}}
\DoxyCodeLine{338                            \textcolor{comment}{! to the open interface width atop a cell [nondim].}}
\DoxyCodeLine{339 \textcolor{keywordtype}{  real} :: BBL\_frac         \textcolor{comment}{! The fraction of a layer's drag that goes into the}}
\DoxyCodeLine{340                            \textcolor{comment}{! viscous bottom boundary layer [nondim].}}
\DoxyCodeLine{341 \textcolor{keywordtype}{  real} :: BBL\_visc\_frac    \textcolor{comment}{! The fraction of all the drag that is expressed as}}
\DoxyCodeLine{342                            \textcolor{comment}{! a viscous bottom boundary layer [nondim].}}
\DoxyCodeLine{343 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{parameter} :: C1\_3 = 1.0/3.0, c1\_6 = 1.0/6.0, c1\_12 = 1.0/12.0}
\DoxyCodeLine{344 \textcolor{keywordtype}{  real} :: C2pi\_3           \textcolor{comment}{! An irrational constant, 2/3 pi.}}
\DoxyCodeLine{345 \textcolor{keywordtype}{  real} :: tmp              \textcolor{comment}{! A temporary variable.}}
\DoxyCodeLine{346 \textcolor{keywordtype}{  real} :: tmp\_val\_m1\_to\_p1}
\DoxyCodeLine{347 \textcolor{keywordtype}{  real} :: curv\_tol         \textcolor{comment}{! Numerator of curvature cubed, used to estimate}}
\DoxyCodeLine{348                            \textcolor{comment}{! accuracy of a single L(:) Newton iteration}}
\DoxyCodeLine{349   \textcolor{keywordtype}{logical} :: use\_L0, do\_one\_L\_iter    \textcolor{comment}{! Control flags for L(:) Newton iteration}}
\DoxyCodeLine{350   \textcolor{keywordtype}{logical} :: use\_BBL\_EOS, do\_i(SZIB\_(G))}
\DoxyCodeLine{351   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(2)} :: EOSdom \textcolor{comment}{! The computational domain for the equation of state}}
\DoxyCodeLine{352   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, Isq, Ieq, Jsq, Jeq, nz, m, n, K2, nkmb, nkml}
\DoxyCodeLine{353   \textcolor{keywordtype}{integer} :: itt, maxitt=20}
\DoxyCodeLine{354   \textcolor{keywordtype}{type}(ocean\_OBC\_type), \textcolor{keywordtype}{pointer} :: OBC => null()}
\DoxyCodeLine{355 }
\DoxyCodeLine{356   is = g\%isc ; ie = g\%iec ; js = g\%jsc ; je = g\%jec ; nz = g\%ke}
\DoxyCodeLine{357   isq = g\%IscB ; ieq = g\%IecB ; jsq = g\%JscB ; jeq = g\%JecB}
\DoxyCodeLine{358   nkmb = gv\%nk\_rho\_varies ; nkml = gv\%nkml}
\DoxyCodeLine{359   h\_neglect = gv\%H\_subroundoff}
\DoxyCodeLine{360   rho0x400\_g = 400.0*(gv\%Rho0 / (us\%L\_to\_Z**2 * gv\%g\_Earth)) * gv\%Z\_to\_H}
\DoxyCodeLine{361   vol\_quit = 0.9*gv\%Angstrom\_H + h\_neglect}
\DoxyCodeLine{362   c2pi\_3 = 8.0*atan(1.0)/3.0}
\DoxyCodeLine{363 }
\DoxyCodeLine{364   \textcolor{keywordflow}{if} (.not.\textcolor{keyword}{associated}(cs)) \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"MOM\_set\_viscosity(BBL): "}//\&}
\DoxyCodeLine{365          \textcolor{stringliteral}{"Module must be initialized before it is used."})}
\DoxyCodeLine{366   \textcolor{keywordflow}{if} (.not.cs\%bottomdraglaw) \textcolor{keywordflow}{return}}
\DoxyCodeLine{367 }
\DoxyCodeLine{368   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(symmetrize)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (symmetrize) \textcolor{keywordflow}{then}}
\DoxyCodeLine{369     jsq = js-\/1 ; isq = is-\/1}
\DoxyCodeLine{370 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{371 }
\DoxyCodeLine{372   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{373     \textcolor{keyword}{call }uvchksum(\textcolor{stringliteral}{"Start set\_viscous\_BBL [uv]"}, u, v, g\%HI, haloshift=1, scale=us\%L\_T\_to\_m\_s)}
\DoxyCodeLine{374     \textcolor{keyword}{call }hchksum(h,\textcolor{stringliteral}{"Start set\_viscous\_BBL h"}, g\%HI, haloshift=1, scale=gv\%H\_to\_m)}
\DoxyCodeLine{375     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%T)) \textcolor{keyword}{call }hchksum(tv\%T, \textcolor{stringliteral}{"Start set\_viscous\_BBL T"}, g\%HI, haloshift=1)}
\DoxyCodeLine{376     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%S)) \textcolor{keyword}{call }hchksum(tv\%S, \textcolor{stringliteral}{"Start set\_viscous\_BBL S"}, g\%HI, haloshift=1)}
\DoxyCodeLine{377 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{378 }
\DoxyCodeLine{379   use\_bbl\_eos = \textcolor{keyword}{associated}(tv\%eqn\_of\_state) .and. cs\%BBL\_use\_EOS}
\DoxyCodeLine{380   obc => cs\%OBC}
\DoxyCodeLine{381 }
\DoxyCodeLine{382   u\_bg\_sq = cs\%drag\_bg\_vel * cs\%drag\_bg\_vel}
\DoxyCodeLine{383   cdrag\_sqrt = sqrt(cs\%cdrag)}
\DoxyCodeLine{384   cdrag\_sqrt\_z = us\%L\_to\_Z * sqrt(cs\%cdrag)}
\DoxyCodeLine{385   k2 = max(nkmb+1, 2)}
\DoxyCodeLine{386 }
\DoxyCodeLine{387 \textcolor{comment}{!  With a linear drag law, the friction velocity is already known.}}
\DoxyCodeLine{388 \textcolor{comment}{!  if (CS\%linear\_drag) ustar(:) = cdrag\_sqrt\_Z*CS\%drag\_bg\_vel}}
\DoxyCodeLine{389 }
\DoxyCodeLine{390   \textcolor{keywordflow}{if} ((nkml>0) .and. .not.use\_bbl\_eos) \textcolor{keywordflow}{then}}
\DoxyCodeLine{391     eosdom(1) = isq -\/ (g\%isd-\/1) ;  eosdom(2) = g\%iec+1 -\/ (g\%isd-\/1)}
\DoxyCodeLine{392     \textcolor{keywordflow}{do} i=isq,ieq+1 ; p\_ref(i) = tv\%P\_Ref ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{393     \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{394     \textcolor{keywordflow}{do} k=1,nkmb ; \textcolor{keywordflow}{do} j=jsq,jeq+1}
\DoxyCodeLine{395       \textcolor{keyword}{call }calculate\_density(tv\%T(:,j,k), tv\%S(:,j,k), p\_ref, rml(:,j,k), tv\%eqn\_of\_state, \&}
\DoxyCodeLine{396                              eosdom)}
\DoxyCodeLine{397 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{398 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{399 }
\DoxyCodeLine{400   \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{401   \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{do} i=is-\/1,ie+1}
\DoxyCodeLine{402     d\_v(i,j) = 0.5*(g\%bathyT(i,j) + g\%bathyT(i,j+1))}
\DoxyCodeLine{403     mask\_v(i,j) = g\%mask2dCv(i,j)}
\DoxyCodeLine{404 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{405   \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{406   \textcolor{keywordflow}{do} j=js-\/1,je+1 ; \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{407     d\_u(i,j) = 0.5*(g\%bathyT(i,j) + g\%bathyT(i+1,j))}
\DoxyCodeLine{408     mask\_u(i,j) = g\%mask2dCu(i,j)}
\DoxyCodeLine{409 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{410 }
\DoxyCodeLine{411   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(obc)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} n=1,obc\%number\_of\_segments}
\DoxyCodeLine{412     \textcolor{keywordflow}{if} (.not. obc\%segment(n)\%on\_pe) cycle}
\DoxyCodeLine{413     \textcolor{comment}{! Use a one-\/sided projection of bottom depths at OBC points.}}
\DoxyCodeLine{414     i = obc\%segment(n)\%HI\%IsdB ; j = obc\%segment(n)\%HI\%JsdB}
\DoxyCodeLine{415     \textcolor{keywordflow}{if} (obc\%segment(n)\%is\_N\_or\_S .and. (j >= js-\/1) .and. (j <= je)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{416       \textcolor{keywordflow}{do} i = max(is-\/1,obc\%segment(n)\%HI\%isd), min(ie+1,obc\%segment(n)\%HI\%ied)}
\DoxyCodeLine{417         \textcolor{keywordflow}{if} (obc\%segment(n)\%direction == obc\_direction\_n) d\_v(i,j) = g\%bathyT(i,j)}
\DoxyCodeLine{418         \textcolor{keywordflow}{if} (obc\%segment(n)\%direction == obc\_direction\_s) d\_v(i,j) = g\%bathyT(i,j+1)}
\DoxyCodeLine{419 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{420     \textcolor{keywordflow}{elseif} (obc\%segment(n)\%is\_E\_or\_W .and. (i >= is-\/1) .and. (i <= ie)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{421       \textcolor{keywordflow}{do} j = max(js-\/1,obc\%segment(n)\%HI\%jsd), min(je+1,obc\%segment(n)\%HI\%jed)}
\DoxyCodeLine{422         \textcolor{keywordflow}{if} (obc\%segment(n)\%direction == obc\_direction\_e) d\_u(i,j) = g\%bathyT(i,j)}
\DoxyCodeLine{423         \textcolor{keywordflow}{if} (obc\%segment(n)\%direction == obc\_direction\_w) d\_u(i,j) = g\%bathyT(i+1,j)}
\DoxyCodeLine{424 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{425 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{426 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{427   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(obc)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} n=1,obc\%number\_of\_segments}
\DoxyCodeLine{428     \textcolor{comment}{! Now project bottom depths across cell-\/corner points in the OBCs.  The two}}
\DoxyCodeLine{429     \textcolor{comment}{! projections have to occur in sequence and can not be combined easily.}}
\DoxyCodeLine{430     \textcolor{keywordflow}{if} (.not. obc\%segment(n)\%on\_pe) cycle}
\DoxyCodeLine{431     \textcolor{comment}{! Use a one-\/sided projection of bottom depths at OBC points.}}
\DoxyCodeLine{432     i = obc\%segment(n)\%HI\%IsdB ; j = obc\%segment(n)\%HI\%JsdB}
\DoxyCodeLine{433     \textcolor{keywordflow}{if} (obc\%segment(n)\%is\_N\_or\_S .and. (j >= js-\/1) .and. (j <= je)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{434       \textcolor{keywordflow}{do} i = max(is-\/1,obc\%segment(n)\%HI\%IsdB), min(ie,obc\%segment(n)\%HI\%IedB)}
\DoxyCodeLine{435         \textcolor{keywordflow}{if} (obc\%segment(n)\%direction == obc\_direction\_n) \textcolor{keywordflow}{then}}
\DoxyCodeLine{436           d\_u(i,j+1) = d\_u(i,j) ;  mask\_u(i,j+1) = 0.0}
\DoxyCodeLine{437         \textcolor{keywordflow}{elseif} (obc\%segment(n)\%direction == obc\_direction\_s) \textcolor{keywordflow}{then}}
\DoxyCodeLine{438           d\_u(i,j) = d\_u(i,j+1) ; mask\_u(i,j) = 0.0}
\DoxyCodeLine{439 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{440 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{441     \textcolor{keywordflow}{elseif} (obc\%segment(n)\%is\_E\_or\_W .and. (i >= is-\/1) .and. (i <= ie)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{442       \textcolor{keywordflow}{do} j = max(js-\/1,obc\%segment(n)\%HI\%JsdB), min(je,obc\%segment(n)\%HI\%JedB)}
\DoxyCodeLine{443         \textcolor{keywordflow}{if} (obc\%segment(n)\%direction == obc\_direction\_e) \textcolor{keywordflow}{then}}
\DoxyCodeLine{444           d\_v(i+1,j) = d\_v(i,j) ; mask\_v(i+1,j) = 0.0}
\DoxyCodeLine{445         \textcolor{keywordflow}{elseif} (obc\%segment(n)\%direction == obc\_direction\_w) \textcolor{keywordflow}{then}}
\DoxyCodeLine{446           d\_v(i,j) = d\_v(i+1,j) ;  mask\_v(i,j) = 0.0}
\DoxyCodeLine{447 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{448 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{449 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{450 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{451 }
\DoxyCodeLine{452   \textcolor{keywordflow}{if} (.not.use\_bbl\_eos) rml\_vel(:,:) = 0.0}
\DoxyCodeLine{453 }
\DoxyCodeLine{454   \textcolor{comment}{!\$OMP parallel do default(private) shared(u,v,h,tv,visc,G,GV,US,CS,Rml,nz,nkmb, \&}}
\DoxyCodeLine{455   \textcolor{comment}{!\$OMP                                     nkml,Isq,Ieq,Jsq,Jeq,h\_neglect,Rho0x400\_G,C2pi\_3, \&}}
\DoxyCodeLine{456   \textcolor{comment}{!\$OMP                                     U\_bg\_sq,cdrag\_sqrt\_Z,cdrag\_sqrt,K2,use\_BBL\_EOS,   \&}}
\DoxyCodeLine{457   \textcolor{comment}{!\$OMP                                     OBC,maxitt,D\_u,D\_v,mask\_u,mask\_v) \&}}
\DoxyCodeLine{458   \textcolor{comment}{!\$OMP                              firstprivate(Vol\_quit)}}
\DoxyCodeLine{459   \textcolor{keywordflow}{do} j=jsq,jeq ; \textcolor{keywordflow}{do} m=1,2}
\DoxyCodeLine{460 }
\DoxyCodeLine{461     \textcolor{keywordflow}{if} (m==1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{462       \textcolor{comment}{! m=1 refers to u-\/points}}
\DoxyCodeLine{463       \textcolor{keywordflow}{if} (j<g\%Jsc) cycle}
\DoxyCodeLine{464       is = isq ; ie = ieq}
\DoxyCodeLine{465       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{466         do\_i(i) = .false.}
\DoxyCodeLine{467         \textcolor{keywordflow}{if} (g\%mask2dCu(i,j) > 0) do\_i(i) = .true.}
\DoxyCodeLine{468 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{469     \textcolor{keywordflow}{else}}
\DoxyCodeLine{470       \textcolor{comment}{! m=2 refers to v-\/points}}
\DoxyCodeLine{471       is = g\%isc ; ie = g\%iec}
\DoxyCodeLine{472       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{473         do\_i(i) = .false.}
\DoxyCodeLine{474         \textcolor{keywordflow}{if} (g\%mask2dCv(i,j) > 0) do\_i(i) = .true.}
\DoxyCodeLine{475 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{476 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{477 }
\DoxyCodeLine{478     \textcolor{comment}{! Calculate thickness at velocity points (u or v depending on value of m).}}
\DoxyCodeLine{479     \textcolor{comment}{! Also interpolate the ML density or T/S properties.}}
\DoxyCodeLine{480     \textcolor{keywordflow}{if} (m==1) \textcolor{keywordflow}{then} \textcolor{comment}{! u-\/points}}
\DoxyCodeLine{481       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{482         \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{483           \textcolor{keywordflow}{if} (u(i,j,k) *(h(i+1,j,k) -\/ h(i,j,k)) >= 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{484             \textcolor{comment}{! If the flow is from thin to thick then bias towards the thinner thickness}}
\DoxyCodeLine{485             h\_at\_vel(i,k) = 2.0*h(i,j,k)*h(i+1,j,k) / \&}
\DoxyCodeLine{486                             (h(i,j,k) + h(i+1,j,k) + h\_neglect)}
\DoxyCodeLine{487           \textcolor{keywordflow}{else}}
\DoxyCodeLine{488             \textcolor{comment}{! If the flow is from thick to thin then use the simple average thickness}}
\DoxyCodeLine{489             h\_at\_vel(i,k) = 0.5 * (h(i,j,k) + h(i+1,j,k))}
\DoxyCodeLine{490 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{491 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{492         h\_vel(i,k) = 0.5 * (h(i,j,k) + h(i+1,j,k))}
\DoxyCodeLine{493 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{494       \textcolor{keywordflow}{if} (use\_bbl\_eos) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{495         \textcolor{comment}{! Perhaps these should be thickness weighted.}}
\DoxyCodeLine{496         t\_vel(i,k) = 0.5 * (tv\%T(i,j,k) + tv\%T(i+1,j,k))}
\DoxyCodeLine{497         s\_vel(i,k) = 0.5 * (tv\%S(i,j,k) + tv\%S(i+1,j,k))}
\DoxyCodeLine{498 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ; \textcolor{keywordflow}{else} ; \textcolor{keywordflow}{do} k=1,nkmb ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{499         rml\_vel(i,k) = 0.5 * (rml(i,j,k) + rml(i+1,j,k))}
\DoxyCodeLine{500 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{501     \textcolor{keywordflow}{else} \textcolor{comment}{! v-\/points}}
\DoxyCodeLine{502       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{503         \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{504           \textcolor{keywordflow}{if} (v(i,j,k) * (h(i,j+1,k) -\/ h(i,j,k)) >= 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{505             \textcolor{comment}{! If the flow is from thin to thick then bias towards the thinner thickness}}
\DoxyCodeLine{506             h\_at\_vel(i,k) = 2.0*h(i,j,k)*h(i,j+1,k) / \&}
\DoxyCodeLine{507                             (h(i,j,k) + h(i,j+1,k) + h\_neglect)}
\DoxyCodeLine{508           \textcolor{keywordflow}{else}}
\DoxyCodeLine{509             \textcolor{comment}{! If the flow is from thick to thin then use the simple average thickness}}
\DoxyCodeLine{510             h\_at\_vel(i,k) = 0.5 * (h(i,j,k) + h(i,j+1,k))}
\DoxyCodeLine{511 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{512 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{513         h\_vel(i,k) = 0.5 * (h(i,j,k) + h(i,j+1,k))}
\DoxyCodeLine{514 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{515       \textcolor{keywordflow}{if} (use\_bbl\_eos) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{516         t\_vel(i,k) = 0.5 * (tv\%T(i,j,k) + tv\%T(i,j+1,k))}
\DoxyCodeLine{517         s\_vel(i,k) = 0.5 * (tv\%S(i,j,k) + tv\%S(i,j+1,k))}
\DoxyCodeLine{518 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ; \textcolor{keywordflow}{else} ; \textcolor{keywordflow}{do} k=1,nkmb ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{519         rml\_vel(i,k) = 0.5 * (rml(i,j,k) + rml(i,j+1,k))}
\DoxyCodeLine{520 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{521 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{522 }
\DoxyCodeLine{523     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(obc)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (obc\%number\_of\_segments > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{524       \textcolor{comment}{! Apply a zero gradient projection of thickness across OBC points.}}
\DoxyCodeLine{525       \textcolor{keywordflow}{if} (m==1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{526         \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i) .and. (obc\%segnum\_u(i,j) /= obc\_none)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{527           \textcolor{keywordflow}{if} (obc\%segment(obc\%segnum\_u(i,j))\%direction == obc\_direction\_e) \textcolor{keywordflow}{then}}
\DoxyCodeLine{528             \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{529               h\_at\_vel(i,k) = h(i,j,k) ; h\_vel(i,k) = h(i,j,k)}
\DoxyCodeLine{530 \textcolor{keywordflow}{            enddo}}
\DoxyCodeLine{531             \textcolor{keywordflow}{if} (use\_bbl\_eos) \textcolor{keywordflow}{then}}
\DoxyCodeLine{532               \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{533                 t\_vel(i,k) = tv\%T(i,j,k) ; s\_vel(i,k) = tv\%S(i,j,k)}
\DoxyCodeLine{534 \textcolor{keywordflow}{              enddo}}
\DoxyCodeLine{535             \textcolor{keywordflow}{else}}
\DoxyCodeLine{536               \textcolor{keywordflow}{do} k=1,nkmb}
\DoxyCodeLine{537                 rml\_vel(i,k) = rml(i,j,k)}
\DoxyCodeLine{538 \textcolor{keywordflow}{              enddo}}
\DoxyCodeLine{539 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{540           \textcolor{keywordflow}{elseif} (obc\%segment(obc\%segnum\_u(i,j))\%direction == obc\_direction\_w) \textcolor{keywordflow}{then}}
\DoxyCodeLine{541             \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{542               h\_at\_vel(i,k) = h(i+1,j,k) ; h\_vel(i,k) = h(i+1,j,k)}
\DoxyCodeLine{543 \textcolor{keywordflow}{            enddo}}
\DoxyCodeLine{544             \textcolor{keywordflow}{if} (use\_bbl\_eos) \textcolor{keywordflow}{then}}
\DoxyCodeLine{545               \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{546                 t\_vel(i,k) = tv\%T(i+1,j,k) ; s\_vel(i,k) = tv\%S(i+1,j,k)}
\DoxyCodeLine{547 \textcolor{keywordflow}{              enddo}}
\DoxyCodeLine{548             \textcolor{keywordflow}{else}}
\DoxyCodeLine{549               \textcolor{keywordflow}{do} k=1,nkmb}
\DoxyCodeLine{550                 rml\_vel(i,k) = rml(i+1,j,k)}
\DoxyCodeLine{551 \textcolor{keywordflow}{              enddo}}
\DoxyCodeLine{552 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{553 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{554 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{555       \textcolor{keywordflow}{else}}
\DoxyCodeLine{556         \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i) .and. (obc\%segnum\_v(i,j) /= obc\_none)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{557           \textcolor{keywordflow}{if} (obc\%segment(obc\%segnum\_v(i,j))\%direction == obc\_direction\_n) \textcolor{keywordflow}{then}}
\DoxyCodeLine{558             \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{559               h\_at\_vel(i,k) = h(i,j,k) ; h\_vel(i,k) = h(i,j,k)}
\DoxyCodeLine{560 \textcolor{keywordflow}{            enddo}}
\DoxyCodeLine{561             \textcolor{keywordflow}{if} (use\_bbl\_eos) \textcolor{keywordflow}{then}}
\DoxyCodeLine{562               \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{563                 t\_vel(i,k) = tv\%T(i,j,k) ; s\_vel(i,k) = tv\%S(i,j,k)}
\DoxyCodeLine{564 \textcolor{keywordflow}{              enddo}}
\DoxyCodeLine{565             \textcolor{keywordflow}{else}}
\DoxyCodeLine{566               \textcolor{keywordflow}{do} k=1,nkmb}
\DoxyCodeLine{567                 rml\_vel(i,k) = rml(i,j,k)}
\DoxyCodeLine{568 \textcolor{keywordflow}{              enddo}}
\DoxyCodeLine{569 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{570           \textcolor{keywordflow}{elseif} (obc\%segment(obc\%segnum\_v(i,j))\%direction == obc\_direction\_s) \textcolor{keywordflow}{then}}
\DoxyCodeLine{571             \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{572               h\_at\_vel(i,k) = h(i,j+1,k) ; h\_vel(i,k) = h(i,j+1,k)}
\DoxyCodeLine{573 \textcolor{keywordflow}{            enddo}}
\DoxyCodeLine{574             \textcolor{keywordflow}{if} (use\_bbl\_eos) \textcolor{keywordflow}{then}}
\DoxyCodeLine{575               \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{576                 t\_vel(i,k) = tv\%T(i,j+1,k) ; s\_vel(i,k) = tv\%S(i,j+1,k)}
\DoxyCodeLine{577 \textcolor{keywordflow}{              enddo}}
\DoxyCodeLine{578             \textcolor{keywordflow}{else}}
\DoxyCodeLine{579               \textcolor{keywordflow}{do} k=1,nkmb}
\DoxyCodeLine{580                 rml\_vel(i,k) = rml(i,j+1,k)}
\DoxyCodeLine{581 \textcolor{keywordflow}{              enddo}}
\DoxyCodeLine{582 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{583 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{584 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{585 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{586 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{587 }
\DoxyCodeLine{588     \textcolor{keywordflow}{if} (use\_bbl\_eos .or. .not.cs\%linear\_drag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{589       \textcolor{comment}{! Calculate the mean velocity magnitude over the bottommost CS\%Hbbl of}}
\DoxyCodeLine{590       \textcolor{comment}{! the water column for determining the quadratic bottom drag.}}
\DoxyCodeLine{591       \textcolor{comment}{! Used in ustar(i)}}
\DoxyCodeLine{592       \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{593         htot\_vel = 0.0 ; hwtot = 0.0 ; hutot = 0.0}
\DoxyCodeLine{594         thtot = 0.0 ; shtot = 0.0}
\DoxyCodeLine{595         \textcolor{keywordflow}{do} k=nz,1,-\/1}
\DoxyCodeLine{596 }
\DoxyCodeLine{597           \textcolor{keywordflow}{if} (htot\_vel>=cs\%Hbbl) \textcolor{keywordflow}{exit} \textcolor{comment}{! terminate the k loop}}
\DoxyCodeLine{598 }
\DoxyCodeLine{599           hweight = min(cs\%Hbbl -\/ htot\_vel, h\_at\_vel(i,k))}
\DoxyCodeLine{600           \textcolor{keywordflow}{if} (hweight < 1.5*gv\%Angstrom\_H + h\_neglect) cycle}
\DoxyCodeLine{601 }
\DoxyCodeLine{602           htot\_vel  = htot\_vel + h\_at\_vel(i,k)}
\DoxyCodeLine{603           hwtot = hwtot + hweight}
\DoxyCodeLine{604 }
\DoxyCodeLine{605           \textcolor{keywordflow}{if} ((.not.cs\%linear\_drag) .and. (hweight >= 0.0)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (m==1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{606             v\_at\_u = set\_v\_at\_u(v, h, g, i, j, k, mask\_v, obc)}
\DoxyCodeLine{607             \textcolor{keywordflow}{if} (cs\%BBL\_use\_tidal\_bg) \textcolor{keywordflow}{then}}
\DoxyCodeLine{608               u\_bg\_sq = 0.5*( g\%mask2dT(i,j)*(cs\%tideamp(i,j)*cs\%tideamp(i,j))+ \&}
\DoxyCodeLine{609                               g\%mask2dT(i+1,j)*(cs\%tideamp(i+1,j)*cs\%tideamp(i+1,j)) )}
\DoxyCodeLine{610 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{611             hutot = hutot + hweight * sqrt(u(i,j,k)*u(i,j,k) + \&}
\DoxyCodeLine{612                                            v\_at\_u*v\_at\_u + u\_bg\_sq)}
\DoxyCodeLine{613           \textcolor{keywordflow}{else}}
\DoxyCodeLine{614             u\_at\_v = set\_u\_at\_v(u, h, g, i, j, k, mask\_u, obc)}
\DoxyCodeLine{615             \textcolor{keywordflow}{if} (cs\%BBL\_use\_tidal\_bg) \textcolor{keywordflow}{then}}
\DoxyCodeLine{616               u\_bg\_sq = 0.5*( g\%mask2dT(i,j)*(cs\%tideamp(i,j)*cs\%tideamp(i,j))+ \&}
\DoxyCodeLine{617                               g\%mask2dT(i,j+1)*(cs\%tideamp(i,j+1)*cs\%tideamp(i,j+1)) )}
\DoxyCodeLine{618 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{619             hutot = hutot + hweight * sqrt(v(i,j,k)*v(i,j,k) + \&}
\DoxyCodeLine{620                                            u\_at\_v*u\_at\_v + u\_bg\_sq)}
\DoxyCodeLine{621 \textcolor{keywordflow}{          endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{622 }
\DoxyCodeLine{623           \textcolor{keywordflow}{if} (use\_bbl\_eos .and. (hweight >= 0.0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{624             thtot = thtot + hweight * t\_vel(i,k)}
\DoxyCodeLine{625             shtot = shtot + hweight * s\_vel(i,k)}
\DoxyCodeLine{626 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{627 \textcolor{keywordflow}{        enddo} \textcolor{comment}{! end of k loop}}
\DoxyCodeLine{628 }
\DoxyCodeLine{629         \textcolor{comment}{! Set u* based on u*\string^2 = Cdrag u\_bbl\string^2}}
\DoxyCodeLine{630         \textcolor{keywordflow}{if} (.not.cs\%linear\_drag .and. (hwtot > 0.0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{631           ustar(i) = cdrag\_sqrt\_z*hutot/hwtot}
\DoxyCodeLine{632         \textcolor{keywordflow}{else}}
\DoxyCodeLine{633           ustar(i) = cdrag\_sqrt\_z*cs\%drag\_bg\_vel}
\DoxyCodeLine{634 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{635 }
\DoxyCodeLine{636         \textcolor{keywordflow}{if} (use\_bbl\_eos) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (hwtot > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{637           t\_eos(i) = thtot/hwtot ; s\_eos(i) = shtot/hwtot}
\DoxyCodeLine{638         \textcolor{keywordflow}{else}}
\DoxyCodeLine{639           t\_eos(i) = 0.0 ; s\_eos(i) = 0.0}
\DoxyCodeLine{640 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{641 }
\DoxyCodeLine{642         \textcolor{comment}{! Diagnostic BBL flow speed at u-\/ and v-\/points.}}
\DoxyCodeLine{643         \textcolor{keywordflow}{if} (cs\%id\_bbl\_u>0 .and. m==1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{644           \textcolor{keywordflow}{if} (hwtot > 0.0) cs\%bbl\_u(i,j) = hutot/hwtot}
\DoxyCodeLine{645         \textcolor{keywordflow}{elseif} (cs\%id\_bbl\_v>0 .and. m==2) \textcolor{keywordflow}{then}}
\DoxyCodeLine{646           \textcolor{keywordflow}{if} (hwtot > 0.0) cs\%bbl\_v(i,j) = hutot/hwtot}
\DoxyCodeLine{647 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{648 }
\DoxyCodeLine{649 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{650     \textcolor{keywordflow}{else}}
\DoxyCodeLine{651       \textcolor{keywordflow}{do} i=is,ie ; ustar(i) = cdrag\_sqrt\_z*cs\%drag\_bg\_vel ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{652 \textcolor{keywordflow}{    endif} \textcolor{comment}{! Not linear\_drag}}
\DoxyCodeLine{653 }
\DoxyCodeLine{654     \textcolor{keywordflow}{if} (use\_bbl\_eos) \textcolor{keywordflow}{then}}
\DoxyCodeLine{655       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%p\_surf)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{656         \textcolor{keywordflow}{if} (m==1) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie ; press(i) = 0.5*(tv\%p\_surf(i,j) + tv\%p\_surf(i+1,j)) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{657         \textcolor{keywordflow}{else} ; \textcolor{keywordflow}{do} i=is,ie ; press(i) = 0.5*(tv\%p\_surf(i,j) + tv\%p\_surf(i,j+1)) ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{658       \textcolor{keywordflow}{else}}
\DoxyCodeLine{659         \textcolor{keywordflow}{do} i=is,ie ; press(i) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{660 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{661       \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (.not.do\_i(i)) \textcolor{keywordflow}{then} ; t\_eos(i) = 0.0 ; s\_eos(i) = 0.0 ;\textcolor{keywordflow}{ endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{662       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{663         press(i) = press(i) + (gv\%H\_to\_RZ*gv\%g\_Earth) * h\_vel(i,k)}
\DoxyCodeLine{664 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{665       \textcolor{keyword}{call }calculate\_density\_derivs(t\_eos, s\_eos, press, dr\_dt, dr\_ds, tv\%eqn\_of\_state, \&}
\DoxyCodeLine{666                                     (/is-\/g\%IsdB+1,ie-\/g\%IsdB+1/) )}
\DoxyCodeLine{667 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{668 }
\DoxyCodeLine{669     \textcolor{comment}{! Find a BBL thickness given by equation 2.20 of Killworth and Edwards, 1999:}}
\DoxyCodeLine{670     \textcolor{comment}{!    ( f h / Cn u* )\string^2 + ( N h / Ci u* ) = 1}}
\DoxyCodeLine{671     \textcolor{comment}{! where Cn=0.5 and Ci=20 (constants suggested by Zilitinkevich and Mironov, 1996).}}
\DoxyCodeLine{672     \textcolor{comment}{! Eq. 2.20 can be expressed in terms of boundary layer thicknesses limited by}}
\DoxyCodeLine{673     \textcolor{comment}{! rotation (h\_f) and stratification (h\_N):}}
\DoxyCodeLine{674     \textcolor{comment}{!    ( h / h\_f )\string^2 + ( h / h\_N ) = 1}}
\DoxyCodeLine{675     \textcolor{comment}{! When stratification dominates h\_N<<h\_f, and vice versa.}}
\DoxyCodeLine{676     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{677       \textcolor{comment}{! The 400.0 in this expression is the square of a Ci introduced in KW99, eq. 2.22.}}
\DoxyCodeLine{678       ustarsq = rho0x400\_g * ustar(i)**2 \textcolor{comment}{! Note not in units of u*\string^2 but [H R]}}
\DoxyCodeLine{679       htot = 0.0}
\DoxyCodeLine{680 }
\DoxyCodeLine{681       \textcolor{comment}{! Calculate the thickness of a stratification limited BBL ignoring rotation:}}
\DoxyCodeLine{682       \textcolor{comment}{!   h\_N = Ci u* / N          (limit of KW99 eq. 2.20 for |f|-\/>0)}}
\DoxyCodeLine{683       \textcolor{comment}{! For layer mode, N\string^2 = g'/h. Since (Ci u*)\string^2 = (h\_N N)\string^2 = h\_N g' then}}
\DoxyCodeLine{684       \textcolor{comment}{!   h\_N = (Ci u*)\string^2 / g'     (KW99, eq, 2.22)}}
\DoxyCodeLine{685       \textcolor{comment}{! Starting from the bottom, integrate the stratification upward until h\_N N balances Ci u*}}
\DoxyCodeLine{686       \textcolor{comment}{! or in layer mode}}
\DoxyCodeLine{687       \textcolor{comment}{!   h\_N Delta rho \string~ (Ci u*)\string^2 rho0 / g}}
\DoxyCodeLine{688       \textcolor{comment}{! where the rhs is stored in variable ustarsq.}}
\DoxyCodeLine{689       \textcolor{comment}{! The method was described in Stephens and Hallberg 2000 (unpublished and lost manuscript).}}
\DoxyCodeLine{690       \textcolor{keywordflow}{if} (use\_bbl\_eos) \textcolor{keywordflow}{then}}
\DoxyCodeLine{691         thtot = 0.0 ; shtot = 0.0 ; oldfn = 0.0}
\DoxyCodeLine{692         \textcolor{keywordflow}{do} k=nz,2,-\/1}
\DoxyCodeLine{693           \textcolor{keywordflow}{if} (h\_at\_vel(i,k) <= 0.0) cycle}
\DoxyCodeLine{694 }
\DoxyCodeLine{695           \textcolor{comment}{! Delta rho * h\_bbl assuming everything below is homogenized}}
\DoxyCodeLine{696           oldfn = dr\_dt(i)*(thtot -\/ t\_vel(i,k)*htot) + \&}
\DoxyCodeLine{697                   dr\_ds(i)*(shtot -\/ s\_vel(i,k)*htot)}
\DoxyCodeLine{698           \textcolor{keywordflow}{if} (oldfn >= ustarsq) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{699 }
\DoxyCodeLine{700           \textcolor{comment}{! Local Delta rho * h\_bbl  at interface}}
\DoxyCodeLine{701           dfn = (dr\_dt(i)*(t\_vel(i,k) -\/ t\_vel(i,k-\/1)) + \&}
\DoxyCodeLine{702                  dr\_ds(i)*(s\_vel(i,k) -\/ s\_vel(i,k-\/1))) * \&}
\DoxyCodeLine{703                 (h\_at\_vel(i,k) + htot)}
\DoxyCodeLine{704 }
\DoxyCodeLine{705           \textcolor{keywordflow}{if} ((oldfn + dfn) <= ustarsq) \textcolor{keywordflow}{then}}
\DoxyCodeLine{706             \textcolor{comment}{! Use whole layer}}
\DoxyCodeLine{707             dh = h\_at\_vel(i,k)}
\DoxyCodeLine{708           \textcolor{keywordflow}{else}}
\DoxyCodeLine{709             \textcolor{comment}{! Use only part of the layer}}
\DoxyCodeLine{710             dh = h\_at\_vel(i,k) * sqrt((ustarsq-\/oldfn) / (dfn))}
\DoxyCodeLine{711 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{712 }
\DoxyCodeLine{713           \textcolor{comment}{! Increment total BBL thickness and cumulative T and S}}
\DoxyCodeLine{714           htot = htot + dh}
\DoxyCodeLine{715           thtot = thtot + t\_vel(i,k)*dh ; shtot = shtot + s\_vel(i,k)*dh}
\DoxyCodeLine{716 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{717         \textcolor{keywordflow}{if} ((oldfn < ustarsq) .and. h\_at\_vel(i,1) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{718           \textcolor{comment}{! Layer 1 might be part of the BBL.}}
\DoxyCodeLine{719           \textcolor{keywordflow}{if} (dr\_dt(i) * (thtot -\/ t\_vel(i,1)*htot) + \&}
\DoxyCodeLine{720               dr\_ds(i) * (shtot -\/ s\_vel(i,1)*htot) < ustarsq) \&}
\DoxyCodeLine{721             htot = htot + h\_at\_vel(i,1)}
\DoxyCodeLine{722 \textcolor{keywordflow}{        endif} \textcolor{comment}{! Examination of layer 1.}}
\DoxyCodeLine{723       \textcolor{keywordflow}{else}  \textcolor{comment}{! Use Rlay and/or the coordinate density as density variables.}}
\DoxyCodeLine{724         rhtot = 0.0}
\DoxyCodeLine{725         \textcolor{keywordflow}{do} k=nz,k2,-\/1}
\DoxyCodeLine{726           oldfn = rhtot -\/ gv\%Rlay(k)*htot}
\DoxyCodeLine{727           dfn = (gv\%Rlay(k) -\/ gv\%Rlay(k-\/1))*(h\_at\_vel(i,k)+htot)}
\DoxyCodeLine{728 }
\DoxyCodeLine{729           \textcolor{keywordflow}{if} (oldfn >= ustarsq) \textcolor{keywordflow}{then}}
\DoxyCodeLine{730             cycle}
\DoxyCodeLine{731           \textcolor{keywordflow}{elseif} ((oldfn + dfn) <= ustarsq) \textcolor{keywordflow}{then}}
\DoxyCodeLine{732             dh = h\_at\_vel(i,k)}
\DoxyCodeLine{733           \textcolor{keywordflow}{else}}
\DoxyCodeLine{734             dh = h\_at\_vel(i,k) * sqrt((ustarsq-\/oldfn) / (dfn))}
\DoxyCodeLine{735 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{736 }
\DoxyCodeLine{737           htot = htot + dh}
\DoxyCodeLine{738           rhtot = rhtot + gv\%Rlay(k)*dh}
\DoxyCodeLine{739 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{740         \textcolor{keywordflow}{if} (nkml>0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{741           \textcolor{keywordflow}{do} k=nkmb,2,-\/1}
\DoxyCodeLine{742             oldfn = rhtot -\/ rml\_vel(i,k)*htot}
\DoxyCodeLine{743             dfn = (rml\_vel(i,k) -\/ rml\_vel(i,k-\/1)) * (h\_at\_vel(i,k)+htot)}
\DoxyCodeLine{744 }
\DoxyCodeLine{745             \textcolor{keywordflow}{if} (oldfn >= ustarsq) \textcolor{keywordflow}{then}}
\DoxyCodeLine{746               cycle}
\DoxyCodeLine{747             \textcolor{keywordflow}{elseif} ((oldfn + dfn) <= ustarsq) \textcolor{keywordflow}{then}}
\DoxyCodeLine{748               dh = h\_at\_vel(i,k)}
\DoxyCodeLine{749             \textcolor{keywordflow}{else}}
\DoxyCodeLine{750               dh = h\_at\_vel(i,k) * sqrt((ustarsq-\/oldfn) / (dfn))}
\DoxyCodeLine{751 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{752 }
\DoxyCodeLine{753             htot = htot + dh}
\DoxyCodeLine{754             rhtot = rhtot + rml\_vel(i,k)*dh}
\DoxyCodeLine{755 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{756           \textcolor{keywordflow}{if} (rhtot -\/ rml\_vel(i,1)*htot < ustarsq) htot = htot + h\_at\_vel(i,1)}
\DoxyCodeLine{757         \textcolor{keywordflow}{else}}
\DoxyCodeLine{758           \textcolor{keywordflow}{if} (rhtot -\/ gv\%Rlay(1)*htot < ustarsq) htot = htot + h\_at\_vel(i,1)}
\DoxyCodeLine{759 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{760 \textcolor{keywordflow}{      endif} \textcolor{comment}{! use\_BBL\_EOS}}
\DoxyCodeLine{761 }
\DoxyCodeLine{762       \textcolor{comment}{! Value of 2*f at u-\/ or v-\/points.}}
\DoxyCodeLine{763       \textcolor{keywordflow}{if} (m==1) \textcolor{keywordflow}{then} ; c2f = g\%CoriolisBu(i,j-\/1) + g\%CoriolisBu(i,j)}
\DoxyCodeLine{764       \textcolor{keywordflow}{else} ; c2f = g\%CoriolisBu(i-\/1,j) + g\%CoriolisBu(i,j) ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{765 }
\DoxyCodeLine{766       \textcolor{comment}{! The thickness of a rotation limited BBL ignoring stratification is}}
\DoxyCodeLine{767       \textcolor{comment}{!   h\_f \string~ Cn u* / f        (limit of KW99 eq. 2.20 for N-\/>0).}}
\DoxyCodeLine{768       \textcolor{comment}{! The buoyancy limit of BBL thickness (h\_N) is already in the variable htot from above.}}
\DoxyCodeLine{769       \textcolor{comment}{! Substituting x = h\_N/h into KW99 eq. 2.20 yields the quadratic}}
\DoxyCodeLine{770       \textcolor{comment}{!   x\string^2 -\/ x = (h\_N / h\_f)\string^2}}
\DoxyCodeLine{771       \textcolor{comment}{! for which the positive root is}}
\DoxyCodeLine{772       \textcolor{comment}{!   xp = 1/2 + sqrt( 1/4 + (h\_N/h\_f)\string^2 )}}
\DoxyCodeLine{773       \textcolor{comment}{! and thus h\_bbl = h\_N / xp . Since h\_f = Cn u*/f and Cn=0.5}}
\DoxyCodeLine{774       \textcolor{comment}{!   xp = 1/2 + sqrt( 1/4 + (2 f h\_N/u*)\string^2 )}}
\DoxyCodeLine{775       \textcolor{comment}{! To avoid dividing by zero if u*=0 then}}
\DoxyCodeLine{776       \textcolor{comment}{!   xp u* = 1/2 u* + sqrt( 1/4 u*\string^2 + (2 f h\_N)\string^2 )}}
\DoxyCodeLine{777       \textcolor{keywordflow}{if} (cs\%cdrag * u\_bg\_sq <= 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{778         \textcolor{comment}{! This avoids NaNs and overflows, and could be used in all cases,}}
\DoxyCodeLine{779         \textcolor{comment}{! but is not bitwise identical to the current code.}}
\DoxyCodeLine{780         usth = ustar(i)*gv\%Z\_to\_H ; root = sqrt(0.25*usth**2 + (htot*c2f)**2)}
\DoxyCodeLine{781         \textcolor{keywordflow}{if} (htot*usth <= (cs\%BBL\_thick\_min+h\_neglect) * (0.5*usth + root)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{782           bbl\_thick = cs\%BBL\_thick\_min}
\DoxyCodeLine{783         \textcolor{keywordflow}{else}}
\DoxyCodeLine{784           \textcolor{comment}{! The following expression reads}}
\DoxyCodeLine{785           \textcolor{comment}{!   h\_bbl = h\_N u* / ( 1/2 u* + sqrt( 1/4 u*\string^2 + ( 2 f h\_N )\string^2 ) )}}
\DoxyCodeLine{786           \textcolor{comment}{! which is h\_bbl = h\_N u*/(xp u*) as described above.}}
\DoxyCodeLine{787           bbl\_thick = (htot * usth) / (0.5*usth + root)}
\DoxyCodeLine{788 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{789       \textcolor{keywordflow}{else}}
\DoxyCodeLine{790         \textcolor{comment}{! The following expression reads}}
\DoxyCodeLine{791         \textcolor{comment}{!   h\_bbl = h\_N / ( 1/2 + sqrt( 1/4 + ( 2 f h\_N / u* )\string^2 ) )}}
\DoxyCodeLine{792         \textcolor{comment}{! which is h\_bbl = h\_N/xp as described above.}}
\DoxyCodeLine{793         bbl\_thick = htot / (0.5 + sqrt(0.25 + htot*htot*c2f*c2f/ \&}
\DoxyCodeLine{794                                        ((ustar(i)*ustar(i)) * (gv\%Z\_to\_H**2)) ) )}
\DoxyCodeLine{795 }
\DoxyCodeLine{796         \textcolor{keywordflow}{if} (bbl\_thick < cs\%BBL\_thick\_min) bbl\_thick = cs\%BBL\_thick\_min}
\DoxyCodeLine{797 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{798 }
\DoxyCodeLine{799       \textcolor{comment}{! If there is Richardson number dependent mixing, that determines}}
\DoxyCodeLine{800       \textcolor{comment}{! the vertical extent of the bottom boundary layer, and there is no}}
\DoxyCodeLine{801       \textcolor{comment}{! need to set that scale here.  In fact, viscously reducing the}}
\DoxyCodeLine{802       \textcolor{comment}{! shears over an excessively large region reduces the efficacy of}}
\DoxyCodeLine{803       \textcolor{comment}{! the Richardson number dependent mixing.}}
\DoxyCodeLine{804       \textcolor{comment}{! In other words, if using RiNo\_mix then CS\%Hbbl acts as an upper bound on}}
\DoxyCodeLine{805       \textcolor{comment}{! bbl\_thick.}}
\DoxyCodeLine{806       \textcolor{keywordflow}{if} ((bbl\_thick > 0.5*cs\%Hbbl) .and. (cs\%RiNo\_mix)) bbl\_thick = 0.5*cs\%Hbbl}
\DoxyCodeLine{807 }
\DoxyCodeLine{808       \textcolor{keywordflow}{if} (cs\%Channel\_drag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{809         \textcolor{comment}{! The drag within the bottommost bbl\_thick is applied as a part of}}
\DoxyCodeLine{810         \textcolor{comment}{! an enhanced bottom viscosity, while above this the drag is applied}}
\DoxyCodeLine{811         \textcolor{comment}{! directly to the layers in question as a Rayleigh drag term.}}
\DoxyCodeLine{812         \textcolor{keywordflow}{if} (m==1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{813           d\_vel = d\_u(i,j)}
\DoxyCodeLine{814           tmp = g\%mask2dCu(i,j+1) * d\_u(i,j+1)}
\DoxyCodeLine{815           dp = 2.0 * d\_vel * tmp / (d\_vel + tmp)}
\DoxyCodeLine{816           tmp = g\%mask2dCu(i,j-\/1) * d\_u(i,j-\/1)}
\DoxyCodeLine{817           dm = 2.0 * d\_vel * tmp / (d\_vel + tmp)}
\DoxyCodeLine{818         \textcolor{keywordflow}{else}}
\DoxyCodeLine{819           d\_vel = d\_v(i,j)}
\DoxyCodeLine{820           tmp = g\%mask2dCv(i+1,j) * d\_v(i+1,j)}
\DoxyCodeLine{821           dp = 2.0 * d\_vel * tmp / (d\_vel + tmp)}
\DoxyCodeLine{822           tmp = g\%mask2dCv(i-\/1,j) * d\_v(i-\/1,j)}
\DoxyCodeLine{823           dm = 2.0 * d\_vel * tmp / (d\_vel + tmp)}
\DoxyCodeLine{824 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{825         \textcolor{keywordflow}{if} (dm > dp) \textcolor{keywordflow}{then} ; tmp = dp ; dp = dm ; dm = tmp ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{826 }
\DoxyCodeLine{827         \textcolor{comment}{! Convert the D's to the units of thickness.}}
\DoxyCodeLine{828         dp = gv\%Z\_to\_H*dp ; dm = gv\%Z\_to\_H*dm ; d\_vel = gv\%Z\_to\_H*d\_vel}
\DoxyCodeLine{829 }
\DoxyCodeLine{830         a\_3 = (dp + dm -\/ 2.0*d\_vel) ; a = 3.0*a\_3 ; a\_12 = 0.25*a\_3}
\DoxyCodeLine{831         slope = dp -\/ dm}
\DoxyCodeLine{832         \textcolor{comment}{! If the curvature is small enough, there is no reason not to assume}}
\DoxyCodeLine{833         \textcolor{comment}{! a uniformly sloping or flat bottom.}}
\DoxyCodeLine{834         \textcolor{keywordflow}{if} (abs(a) < 1e-\/2*(slope + cs\%BBL\_thick\_min)) a = 0.0}
\DoxyCodeLine{835         \textcolor{comment}{! Each cell extends from x=-\/1/2 to 1/2, and has a topography}}
\DoxyCodeLine{836         \textcolor{comment}{! given by D(x) = a*x\string^2 + b*x + D -\/ a/12.}}
\DoxyCodeLine{837 }
\DoxyCodeLine{838         \textcolor{comment}{! Calculate the volume above which the entire cell is open and the}}
\DoxyCodeLine{839         \textcolor{comment}{! other volumes at which the equation that is solved for L changes.}}
\DoxyCodeLine{840         \textcolor{keywordflow}{if} (a > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{841           \textcolor{keywordflow}{if} (slope >= a) \textcolor{keywordflow}{then}}
\DoxyCodeLine{842             vol\_open = d\_vel -\/ dm ; vol\_2\_reg = vol\_open}
\DoxyCodeLine{843           \textcolor{keywordflow}{else}}
\DoxyCodeLine{844             tmp = slope/a}
\DoxyCodeLine{845             vol\_open = 0.25*slope*tmp + c1\_12*a}
\DoxyCodeLine{846             vol\_2\_reg = 0.5*tmp**2 * (a -\/ c1\_3*slope)}
\DoxyCodeLine{847 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{848           \textcolor{comment}{! Define some combinations of a \& b for later use.}}
\DoxyCodeLine{849           c24\_a = 24.0/a ; iapb = 1.0/(a+slope)}
\DoxyCodeLine{850           apb\_4a = (slope+a)/(4.0*a) ; a2x48\_apb3 = (48.0*(a*a))*(iapb**3)}
\DoxyCodeLine{851           ax2\_3apb = 2.0*c1\_3*a*iapb}
\DoxyCodeLine{852         \textcolor{keywordflow}{elseif} (a == 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{853           vol\_open = 0.5*slope}
\DoxyCodeLine{854           \textcolor{keywordflow}{if} (slope > 0) iapb = 1.0/slope}
\DoxyCodeLine{855         \textcolor{keywordflow}{else} \textcolor{comment}{! a < 0.0}}
\DoxyCodeLine{856           vol\_open = d\_vel -\/ dm}
\DoxyCodeLine{857           \textcolor{keywordflow}{if} (slope >= -\/a) \textcolor{keywordflow}{then}}
\DoxyCodeLine{858             iapb = 1.0e30 ; \textcolor{keywordflow}{if} (slope+a /= 0.0) iapb = 1.0/(a+slope)}
\DoxyCodeLine{859             vol\_direct = 0.0 ; l\_direct = 0.0 ; c24\_a = 0.0}
\DoxyCodeLine{860           \textcolor{keywordflow}{else}}
\DoxyCodeLine{861             c24\_a = 24.0/a ; iapb = 1.0/(a+slope)}
\DoxyCodeLine{862             l\_direct = 1.0 + slope/a \textcolor{comment}{! L\_direct < 1 because a < 0}}
\DoxyCodeLine{863             vol\_direct = -\/c1\_6*a*l\_direct**3}
\DoxyCodeLine{864 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{865           ibma\_2 = 2.0 / (slope -\/ a)}
\DoxyCodeLine{866 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{867 }
\DoxyCodeLine{868         l(nz+1) = 0.0 ; vol = 0.0 ; vol\_err = 0.0 ; bbl\_visc\_frac = 0.0}
\DoxyCodeLine{869         \textcolor{comment}{! Determine the normalized open length at each interface.}}
\DoxyCodeLine{870         \textcolor{keywordflow}{do} k=nz,1,-\/1}
\DoxyCodeLine{871           vol\_below = vol}
\DoxyCodeLine{872 }
\DoxyCodeLine{873           vol = vol + h\_vel(i,k)}
\DoxyCodeLine{874           h\_vel\_pos = h\_vel(i,k) + h\_neglect}
\DoxyCodeLine{875 }
\DoxyCodeLine{876           \textcolor{keywordflow}{if} (vol >= vol\_open) \textcolor{keywordflow}{then} ; l(k) = 1.0}
\DoxyCodeLine{877           \textcolor{keywordflow}{elseif} (a == 0) \textcolor{keywordflow}{then} \textcolor{comment}{! The bottom has no curvature.}}
\DoxyCodeLine{878             l(k) = sqrt(2.0*vol*iapb)}
\DoxyCodeLine{879           \textcolor{keywordflow}{elseif} (a > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{880             \textcolor{comment}{! There may be a minimum depth, and there are}}
\DoxyCodeLine{881             \textcolor{comment}{! analytic expressions for L for all cases.}}
\DoxyCodeLine{882             \textcolor{keywordflow}{if} (vol < vol\_2\_reg) \textcolor{keywordflow}{then}}
\DoxyCodeLine{883               \textcolor{comment}{! In this case, there is a contiguous open region and}}
\DoxyCodeLine{884               \textcolor{comment}{!   vol = 0.5*L\string^2*(slope + a/3*(3-\/4L)).}}
\DoxyCodeLine{885               \textcolor{keywordflow}{if} (a2x48\_apb3*vol < 1e-\/8) \textcolor{keywordflow}{then} \textcolor{comment}{! Could be 1e-\/7?}}
\DoxyCodeLine{886                 \textcolor{comment}{! There is a very good approximation here for massless layers.}}
\DoxyCodeLine{887                 l0 = sqrt(2.0*vol*iapb) ; l(k) = l0*(1.0 + ax2\_3apb*l0)}
\DoxyCodeLine{888               \textcolor{keywordflow}{else}}
\DoxyCodeLine{889                 l(k) = apb\_4a * (1.0 -\/ \&}
\DoxyCodeLine{890                          2.0 * cos(c1\_3*acos(a2x48\_apb3*vol -\/ 1.0) -\/ c2pi\_3))}
\DoxyCodeLine{891 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{892               \textcolor{comment}{! To check the answers.}}
\DoxyCodeLine{893               \textcolor{comment}{! Vol\_err = 0.5*(L(K)*L(K))*(slope + a\_3*(3.0-\/4.0*L(K))) -\/ vol}}
\DoxyCodeLine{894             \textcolor{keywordflow}{else} \textcolor{comment}{! There are two separate open regions.}}
\DoxyCodeLine{895               \textcolor{comment}{!   vol = slope\string^2/4a + a/12 -\/ (a/12)*(1-\/L)\string^2*(1+2L)}}
\DoxyCodeLine{896               \textcolor{comment}{! At the deepest volume, L = slope/a, at the top L = 1.}}
\DoxyCodeLine{897               \textcolor{comment}{!L(K) = 0.5 -\/ cos(C1\_3*acos(1.0 -\/ C24\_a*(Vol\_open -\/ vol)) -\/ C2pi\_3)}}
\DoxyCodeLine{898               tmp\_val\_m1\_to\_p1 = 1.0 -\/ c24\_a*(vol\_open -\/ vol)}
\DoxyCodeLine{899               tmp\_val\_m1\_to\_p1 = max(-\/1., min(1., tmp\_val\_m1\_to\_p1))}
\DoxyCodeLine{900               l(k) = 0.5 -\/ cos(c1\_3*acos(tmp\_val\_m1\_to\_p1) -\/ c2pi\_3)}
\DoxyCodeLine{901               \textcolor{comment}{! To check the answers.}}
\DoxyCodeLine{902               \textcolor{comment}{! Vol\_err = Vol\_open -\/ a\_12*(1.0+2.0*L(K)) * (1.0-\/L(K))**2 -\/ vol}}
\DoxyCodeLine{903 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{904           \textcolor{keywordflow}{else} \textcolor{comment}{! a < 0.}}
\DoxyCodeLine{905             \textcolor{keywordflow}{if} (vol <= vol\_direct) \textcolor{keywordflow}{then}}
\DoxyCodeLine{906               \textcolor{comment}{! Both edges of the cell are bounded by walls.}}
\DoxyCodeLine{907               l(k) = (-\/0.25*c24\_a*vol)**c1\_3}
\DoxyCodeLine{908             \textcolor{keywordflow}{else}}
\DoxyCodeLine{909               \textcolor{comment}{! x\_R is at 1/2 but x\_L is in the interior \& L is found by solving}}
\DoxyCodeLine{910               \textcolor{comment}{!   vol = 0.5*L\string^2*(slope + a/3*(3-\/4L))}}
\DoxyCodeLine{911 }
\DoxyCodeLine{912               \textcolor{comment}{!  Vol\_err = 0.5*(L(K+1)*L(K+1))*(slope + a\_3*(3.0-\/4.0*L(K+1))) -\/ vol\_below}}
\DoxyCodeLine{913               \textcolor{comment}{! Change to ...}}
\DoxyCodeLine{914               \textcolor{comment}{!   if (min(Vol\_below + Vol\_err, vol) <= Vol\_direct) then ?}}
\DoxyCodeLine{915               \textcolor{keywordflow}{if} (vol\_below + vol\_err <= vol\_direct) \textcolor{keywordflow}{then}}
\DoxyCodeLine{916                 l0 = l\_direct ; vol\_0 = vol\_direct}
\DoxyCodeLine{917               \textcolor{keywordflow}{else}}
\DoxyCodeLine{918                 l0 = l(k+1) ; vol\_0 = vol\_below + vol\_err}
\DoxyCodeLine{919                 \textcolor{comment}{! Change to   Vol\_0 = min(Vol\_below + Vol\_err, vol) ?}}
\DoxyCodeLine{920 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{921 }
\DoxyCodeLine{922               \textcolor{comment}{!   Try a relatively simple solution that usually works well}}
\DoxyCodeLine{923               \textcolor{comment}{! for massless layers.}}
\DoxyCodeLine{924               dv\_dl2 = 0.5*(slope+a) -\/ a*l0 ; dvol = (vol-\/vol\_0)}
\DoxyCodeLine{925            \textcolor{comment}{!  dV\_dL2 = 0.5*(slope+a) -\/ a*L0 ; dVol = max(vol-\/Vol\_0, 0.0)}}
\DoxyCodeLine{926 }
\DoxyCodeLine{927               use\_l0 = .false.}
\DoxyCodeLine{928               do\_one\_l\_iter = .false.}
\DoxyCodeLine{929               \textcolor{keywordflow}{if} (cs\%answers\_2018) \textcolor{keywordflow}{then}}
\DoxyCodeLine{930                 curv\_tol = gv\%Angstrom\_H*dv\_dl2**2 \&}
\DoxyCodeLine{931                            * (0.25 * dv\_dl2 * gv\%Angstrom\_H -\/ a * l0 * dvol)}
\DoxyCodeLine{932                 do\_one\_l\_iter = (a * a * dvol**3) < curv\_tol}
\DoxyCodeLine{933               \textcolor{keywordflow}{else}}
\DoxyCodeLine{934                 \textcolor{comment}{! The following code is more robust when GV\%Angstrom\_H=0, but}}
\DoxyCodeLine{935                 \textcolor{comment}{! it changes answers.}}
\DoxyCodeLine{936                 use\_l0 = (dvol <= 0.)}
\DoxyCodeLine{937 }
\DoxyCodeLine{938                 vol\_tol = max(0.5 * gv\%Angstrom\_H + gv\%H\_subroundoff, 1e-\/14 * vol)}
\DoxyCodeLine{939                 vol\_quit = max(0.9 * gv\%Angstrom\_H + gv\%H\_subroundoff, 1e-\/14 * vol)}
\DoxyCodeLine{940 }
\DoxyCodeLine{941                 curv\_tol = vol\_tol * dv\_dl2**2 \&}
\DoxyCodeLine{942                            * (dv\_dl2 * vol\_tol -\/ 2.0 * a * l0 * dvol)}
\DoxyCodeLine{943                 do\_one\_l\_iter = (a * a * dvol**3) < curv\_tol}
\DoxyCodeLine{944 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{945 }
\DoxyCodeLine{946               \textcolor{keywordflow}{if} (use\_l0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{947                 l(k) = l0}
\DoxyCodeLine{948                 vol\_err = 0.5*(l(k)*l(k))*(slope + a\_3*(3.0-\/4.0*l(k))) -\/ vol}
\DoxyCodeLine{949               \textcolor{keywordflow}{elseif} (do\_one\_l\_iter) \textcolor{keywordflow}{then}}
\DoxyCodeLine{950                 \textcolor{comment}{! One iteration of Newton's method should give an estimate}}
\DoxyCodeLine{951                 \textcolor{comment}{! that is accurate to within Vol\_tol.}}
\DoxyCodeLine{952                 l(k) = sqrt(l0*l0 + dvol / dv\_dl2)}
\DoxyCodeLine{953                 vol\_err = 0.5*(l(k)*l(k))*(slope + a\_3*(3.0-\/4.0*l(k))) -\/ vol}
\DoxyCodeLine{954               \textcolor{keywordflow}{else}}
\DoxyCodeLine{955                 \textcolor{keywordflow}{if} (dv\_dl2*(1.0-\/l0*l0) < dvol + \&}
\DoxyCodeLine{956                     dv\_dl2 * (vol\_open -\/ vol)*ibma\_2) \textcolor{keywordflow}{then}}
\DoxyCodeLine{957                   l\_max = sqrt(1.0 -\/ (vol\_open -\/ vol)*ibma\_2)}
\DoxyCodeLine{958                 \textcolor{keywordflow}{else}}
\DoxyCodeLine{959                   l\_max = sqrt(l0*l0 + dvol / dv\_dl2)}
\DoxyCodeLine{960 \textcolor{keywordflow}{                endif}}
\DoxyCodeLine{961                 l\_min = sqrt(l0*l0 + dvol / (0.5*(slope+a) -\/ a*l\_max))}
\DoxyCodeLine{962 }
\DoxyCodeLine{963                 vol\_err\_min = 0.5*(l\_min**2)*(slope + a\_3*(3.0-\/4.0*l\_min)) -\/ vol}
\DoxyCodeLine{964                 vol\_err\_max = 0.5*(l\_max**2)*(slope + a\_3*(3.0-\/4.0*l\_max)) -\/ vol}
\DoxyCodeLine{965            \textcolor{comment}{!    if ((abs(Vol\_err\_min) <= Vol\_quit) .or. (Vol\_err\_min >= Vol\_err\_max)) then}}
\DoxyCodeLine{966                 \textcolor{keywordflow}{if} (abs(vol\_err\_min) <= vol\_quit) \textcolor{keywordflow}{then}}
\DoxyCodeLine{967                   l(k) = l\_min ; vol\_err = vol\_err\_min}
\DoxyCodeLine{968                 \textcolor{keywordflow}{else}}
\DoxyCodeLine{969                   l(k) = sqrt((l\_min**2*vol\_err\_max -\/ l\_max**2*vol\_err\_min) / \&}
\DoxyCodeLine{970                               (vol\_err\_max -\/ vol\_err\_min))}
\DoxyCodeLine{971                   \textcolor{keywordflow}{do} itt=1,maxitt}
\DoxyCodeLine{972                     vol\_err = 0.5*(l(k)*l(k))*(slope + a\_3*(3.0-\/4.0*l(k))) -\/ vol}
\DoxyCodeLine{973                     \textcolor{keywordflow}{if} (abs(vol\_err) <= vol\_quit) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{974                     \textcolor{comment}{! Take a Newton's method iteration. This equation has proven}}
\DoxyCodeLine{975                     \textcolor{comment}{! robust enough not to need bracketing.}}
\DoxyCodeLine{976                     l(k) = l(k) -\/ vol\_err / (l(k)* (slope + a -\/ 2.0*a*l(k)))}
\DoxyCodeLine{977                     \textcolor{comment}{! This would be a Newton's method iteration for L\string^2:}}
\DoxyCodeLine{978                     \textcolor{comment}{!   L(K) = sqrt(L(K)*L(K) -\/ Vol\_err / (0.5*(slope+a) -\/ a*L(K)))}}
\DoxyCodeLine{979 \textcolor{keywordflow}{                  enddo}}
\DoxyCodeLine{980 \textcolor{keywordflow}{                endif} \textcolor{comment}{! end of iterative solver}}
\DoxyCodeLine{981 \textcolor{keywordflow}{              endif} \textcolor{comment}{! end of 1-\/boundary alternatives.}}
\DoxyCodeLine{982 \textcolor{keywordflow}{            endif} \textcolor{comment}{! end of a<0 cases.}}
\DoxyCodeLine{983 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{984 }
\DoxyCodeLine{985           \textcolor{comment}{! Determine the drag contributing to the bottom boundary layer}}
\DoxyCodeLine{986           \textcolor{comment}{! and the Raleigh drag that acts on each layer.}}
\DoxyCodeLine{987           \textcolor{keywordflow}{if} (l(k) > l(k+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{988             \textcolor{keywordflow}{if} (vol\_below < bbl\_thick) \textcolor{keywordflow}{then}}
\DoxyCodeLine{989               bbl\_frac = (1.0-\/vol\_below/bbl\_thick)**2}
\DoxyCodeLine{990               bbl\_visc\_frac = bbl\_visc\_frac + bbl\_frac*(l(k) -\/ l(k+1))}
\DoxyCodeLine{991             \textcolor{keywordflow}{else}}
\DoxyCodeLine{992               bbl\_frac = 0.0}
\DoxyCodeLine{993 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{994 }
\DoxyCodeLine{995             \textcolor{keywordflow}{if} (m==1) \textcolor{keywordflow}{then} ; cell\_width = g\%dy\_Cu(i,j)}
\DoxyCodeLine{996             \textcolor{keywordflow}{else} ; cell\_width = g\%dx\_Cv(i,j) ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{997             gam = 1.0 -\/ l(k+1)/l(k)}
\DoxyCodeLine{998             rayleigh = us\%L\_to\_Z * cs\%cdrag * (l(k)-\/l(k+1)) * (1.0-\/bbl\_frac) * \&}
\DoxyCodeLine{999                 (12.0*cs\%c\_Smag*h\_vel\_pos) /  (12.0*cs\%c\_Smag*h\_vel\_pos + \&}
\DoxyCodeLine{1000                  us\%L\_to\_Z*gv\%Z\_to\_H * cs\%cdrag * gam*(1.0-\/gam)*(1.0-\/1.5*gam) * l(k)**2 * cell\_width)}
\DoxyCodeLine{1001           \textcolor{keywordflow}{else} \textcolor{comment}{! This layer feels no drag.}}
\DoxyCodeLine{1002             rayleigh = 0.0}
\DoxyCodeLine{1003 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1004 }
\DoxyCodeLine{1005           \textcolor{keywordflow}{if} (m==1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1006             \textcolor{keywordflow}{if} (rayleigh > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1007               v\_at\_u = set\_v\_at\_u(v, h, g, i, j, k, mask\_v, obc)}
\DoxyCodeLine{1008               visc\%Ray\_u(i,j,k) = rayleigh*sqrt(u(i,j,k)*u(i,j,k) + \&}
\DoxyCodeLine{1009                                                 v\_at\_u*v\_at\_u + u\_bg\_sq)}
\DoxyCodeLine{1010             \textcolor{keywordflow}{else} ; visc\%Ray\_u(i,j,k) = 0.0 ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1011           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1012             \textcolor{keywordflow}{if} (rayleigh > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1013               u\_at\_v = set\_u\_at\_v(u, h, g, i, j, k, mask\_u, obc)}
\DoxyCodeLine{1014               visc\%Ray\_v(i,j,k) = rayleigh*sqrt(v(i,j,k)*v(i,j,k) + \&}
\DoxyCodeLine{1015                                                 u\_at\_v*u\_at\_v + u\_bg\_sq)}
\DoxyCodeLine{1016             \textcolor{keywordflow}{else} ; visc\%Ray\_v(i,j,k) = 0.0 ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1017 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1018 }
\DoxyCodeLine{1019 \textcolor{keywordflow}{        enddo} \textcolor{comment}{! k loop to determine L(K).}}
\DoxyCodeLine{1020 }
\DoxyCodeLine{1021         \textcolor{comment}{! Set the near-\/bottom viscosity to a value which will give}}
\DoxyCodeLine{1022         \textcolor{comment}{! the correct stress when the shear occurs over bbl\_thick.}}
\DoxyCodeLine{1023         \textcolor{comment}{! See next block for explanation.}}
\DoxyCodeLine{1024         bbl\_thick\_z = bbl\_thick * gv\%H\_to\_Z}
\DoxyCodeLine{1025         \textcolor{keywordflow}{if} (cs\%correct\_BBL\_bounds .and. \&}
\DoxyCodeLine{1026             cdrag\_sqrt*ustar(i)*bbl\_thick\_z*bbl\_visc\_frac <= cs\%Kv\_BBL\_min) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1027           \textcolor{comment}{! If the bottom stress implies less viscosity than Kv\_BBL\_min then}}
\DoxyCodeLine{1028           \textcolor{comment}{! set kv\_bbl to the bound and recompute bbl\_thick to be consistent}}
\DoxyCodeLine{1029           \textcolor{comment}{! but with a ridiculously large upper bound on thickness (for Cd u*=0)}}
\DoxyCodeLine{1030           kv\_bbl = cs\%Kv\_BBL\_min}
\DoxyCodeLine{1031           \textcolor{keywordflow}{if} (cdrag\_sqrt*ustar(i)*bbl\_visc\_frac*g\%Rad\_Earth*us\%m\_to\_Z > kv\_bbl) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1032             bbl\_thick\_z = kv\_bbl / ( cdrag\_sqrt*ustar(i)*bbl\_visc\_frac )}
\DoxyCodeLine{1033           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1034             bbl\_thick\_z = g\%Rad\_Earth * us\%m\_to\_Z}
\DoxyCodeLine{1035 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1036         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1037           kv\_bbl = cdrag\_sqrt*ustar(i)*bbl\_thick\_z*bbl\_visc\_frac}
\DoxyCodeLine{1038 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1039 }
\DoxyCodeLine{1040       \textcolor{keywordflow}{else} \textcolor{comment}{! Not Channel\_drag.}}
\DoxyCodeLine{1041         \textcolor{comment}{! Set the near-\/bottom viscosity to a value which will give}}
\DoxyCodeLine{1042         \textcolor{comment}{! the correct stress when the shear occurs over bbl\_thick.}}
\DoxyCodeLine{1043         \textcolor{comment}{! -\/ The bottom stress is tau\_b = Cdrag * u\_bbl\string^2}}
\DoxyCodeLine{1044         \textcolor{comment}{! -\/ u\_bbl was calculated by averaging flow over CS\%Hbbl}}
\DoxyCodeLine{1045         \textcolor{comment}{!   (and includes unresolved tidal components)}}
\DoxyCodeLine{1046         \textcolor{comment}{! -\/ u\_bbl is embedded in u* since u*\string^2 = Cdrag u\_bbl\string^2}}
\DoxyCodeLine{1047         \textcolor{comment}{! -\/ The average shear in the BBL is du/dz = 2 * u\_bbl / h\_bbl}}
\DoxyCodeLine{1048         \textcolor{comment}{!   (which assumes a linear profile, hence the "2")}}
\DoxyCodeLine{1049         \textcolor{comment}{! -\/ bbl\_thick was bounded to <= 0.5 * CS\%Hbbl}}
\DoxyCodeLine{1050         \textcolor{comment}{! -\/ The viscous stress kv\_bbl du/dz should balance tau\_b}}
\DoxyCodeLine{1051         \textcolor{comment}{!      Cdrag u\_bbl\string^2 = kv\_bbl du/dz}}
\DoxyCodeLine{1052         \textcolor{comment}{!                    = 2 kv\_bbl u\_bbl}}
\DoxyCodeLine{1053         \textcolor{comment}{! so}}
\DoxyCodeLine{1054         \textcolor{comment}{!      kv\_bbl = 0.5 h\_bbl Cdrag u\_bbl}}
\DoxyCodeLine{1055         \textcolor{comment}{!             = 0.5 h\_bbl sqrt(Cdrag) u*}}
\DoxyCodeLine{1056         bbl\_thick\_z = bbl\_thick * gv\%H\_to\_Z}
\DoxyCodeLine{1057         \textcolor{keywordflow}{if} (cs\%correct\_BBL\_bounds .and. \&}
\DoxyCodeLine{1058             cdrag\_sqrt*ustar(i)*bbl\_thick\_z <= cs\%Kv\_BBL\_min) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1059           \textcolor{comment}{! If the bottom stress implies less viscosity than Kv\_BBL\_min then}}
\DoxyCodeLine{1060           \textcolor{comment}{! set kv\_bbl to the bound and recompute bbl\_thick to be consistent}}
\DoxyCodeLine{1061           \textcolor{comment}{! but with a ridiculously large upper bound on thickness (for Cd u*=0)}}
\DoxyCodeLine{1062           kv\_bbl = cs\%Kv\_BBL\_min}
\DoxyCodeLine{1063           \textcolor{keywordflow}{if} (cdrag\_sqrt*ustar(i)*g\%Rad\_Earth*us\%m\_to\_Z > kv\_bbl) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1064             bbl\_thick\_z = kv\_bbl / ( cdrag\_sqrt*ustar(i) )}
\DoxyCodeLine{1065           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1066             bbl\_thick\_z = g\%Rad\_Earth * us\%m\_to\_Z}
\DoxyCodeLine{1067 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1068         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1069           kv\_bbl = cdrag\_sqrt*ustar(i)*bbl\_thick\_z}
\DoxyCodeLine{1070 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1071 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1072       kv\_bbl = max(cs\%Kv\_BBL\_min, kv\_bbl)}
\DoxyCodeLine{1073       \textcolor{keywordflow}{if} (m==1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1074         visc\%Kv\_bbl\_u(i,j) = kv\_bbl}
\DoxyCodeLine{1075         visc\%bbl\_thick\_u(i,j) = bbl\_thick\_z}
\DoxyCodeLine{1076       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1077         visc\%Kv\_bbl\_v(i,j) = kv\_bbl}
\DoxyCodeLine{1078         visc\%bbl\_thick\_v(i,j) = bbl\_thick\_z}
\DoxyCodeLine{1079 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1080 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! end of i loop}}
\DoxyCodeLine{1081 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! end of m \& j loops}}
\DoxyCodeLine{1082 }
\DoxyCodeLine{1083 \textcolor{comment}{! Offer diagnostics for averaging}}
\DoxyCodeLine{1084   \textcolor{keywordflow}{if} (cs\%id\_bbl\_thick\_u > 0) \&}
\DoxyCodeLine{1085     \textcolor{keyword}{call }post\_data(cs\%id\_bbl\_thick\_u, visc\%bbl\_thick\_u, cs\%diag)}
\DoxyCodeLine{1086   \textcolor{keywordflow}{if} (cs\%id\_kv\_bbl\_u > 0) \&}
\DoxyCodeLine{1087     \textcolor{keyword}{call }post\_data(cs\%id\_kv\_bbl\_u, visc\%kv\_bbl\_u, cs\%diag)}
\DoxyCodeLine{1088   \textcolor{keywordflow}{if} (cs\%id\_bbl\_u > 0) \&}
\DoxyCodeLine{1089     \textcolor{keyword}{call }post\_data(cs\%id\_bbl\_u, cs\%bbl\_u, cs\%diag)}
\DoxyCodeLine{1090   \textcolor{keywordflow}{if} (cs\%id\_bbl\_thick\_v > 0) \&}
\DoxyCodeLine{1091     \textcolor{keyword}{call }post\_data(cs\%id\_bbl\_thick\_v, visc\%bbl\_thick\_v, cs\%diag)}
\DoxyCodeLine{1092   \textcolor{keywordflow}{if} (cs\%id\_kv\_bbl\_v > 0) \&}
\DoxyCodeLine{1093     \textcolor{keyword}{call }post\_data(cs\%id\_kv\_bbl\_v, visc\%kv\_bbl\_v, cs\%diag)}
\DoxyCodeLine{1094   \textcolor{keywordflow}{if} (cs\%id\_bbl\_v > 0) \&}
\DoxyCodeLine{1095     \textcolor{keyword}{call }post\_data(cs\%id\_bbl\_v, cs\%bbl\_v, cs\%diag)}
\DoxyCodeLine{1096   \textcolor{keywordflow}{if} (cs\%id\_Ray\_u > 0) \&}
\DoxyCodeLine{1097     \textcolor{keyword}{call }post\_data(cs\%id\_Ray\_u, visc\%Ray\_u, cs\%diag)}
\DoxyCodeLine{1098   \textcolor{keywordflow}{if} (cs\%id\_Ray\_v > 0) \&}
\DoxyCodeLine{1099     \textcolor{keyword}{call }post\_data(cs\%id\_Ray\_v, visc\%Ray\_v, cs\%diag)}
\DoxyCodeLine{1100 }
\DoxyCodeLine{1101   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1102     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%Ray\_u) .and. \textcolor{keyword}{associated}(visc\%Ray\_v)) \&}
\DoxyCodeLine{1103         \textcolor{keyword}{call }uvchksum(\textcolor{stringliteral}{"Ray [uv]"}, visc\%Ray\_u, visc\%Ray\_v, g\%HI, haloshift=0, scale=us\%Z\_to\_m*us\%s\_to\_T)}
\DoxyCodeLine{1104     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%kv\_bbl\_u) .and. \textcolor{keyword}{associated}(visc\%kv\_bbl\_v)) \&}
\DoxyCodeLine{1105         \textcolor{keyword}{call }uvchksum(\textcolor{stringliteral}{"kv\_bbl\_[uv]"}, visc\%kv\_bbl\_u, visc\%kv\_bbl\_v, g\%HI, \&}
\DoxyCodeLine{1106                       haloshift=0, scale=us\%Z2\_T\_to\_m2\_s, scalar\_pair=.true.)}
\DoxyCodeLine{1107     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%bbl\_thick\_u) .and. \textcolor{keyword}{associated}(visc\%bbl\_thick\_v)) \&}
\DoxyCodeLine{1108         \textcolor{keyword}{call }uvchksum(\textcolor{stringliteral}{"bbl\_thick\_[uv]"}, visc\%bbl\_thick\_u, visc\%bbl\_thick\_v, \&}
\DoxyCodeLine{1109                       g\%HI, haloshift=0, scale=us\%Z\_to\_m, scalar\_pair=.true.)}
\DoxyCodeLine{1110 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1111 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__set__visc_aba41cd4f8baa1cda9036d97087ce8a22}\label{namespacemom__set__visc_aba41cd4f8baa1cda9036d97087ce8a22}} 
\index{mom\_set\_visc@{mom\_set\_visc}!set\_viscous\_ml@{set\_viscous\_ml}}
\index{set\_viscous\_ml@{set\_viscous\_ml}!mom\_set\_visc@{mom\_set\_visc}}
\doxysubsubsection{\texorpdfstring{set\_viscous\_ml()}{set\_viscous\_ml()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+set\+\_\+visc\+::set\+\_\+viscous\+\_\+ml (\begin{DoxyParamCaption}\item[{real, dimension( g \%isdb\+: g \%iedb, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{u,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsdb\+: g \%jedb, g \%ke), intent(in)}]{v,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{h,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(in)}]{tv,  }\item[{type(mech\+\_\+forcing), intent(in)}]{forces,  }\item[{type(vertvisc\+\_\+type), intent(inout)}]{visc,  }\item[{real, intent(in)}]{dt,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(inout)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__set__visc_1_1set__visc__cs}{set\+\_\+visc\+\_\+cs}}), pointer}]{CS,  }\item[{logical, intent(in), optional}]{symmetrize }\end{DoxyParamCaption})}



Calculates the thickness of the surface boundary layer for applying an elevated viscosity. 

A bulk Richardson criterion or the thickness of the topmost N\+K\+ML layers (with a bulk mixed layer) are currently used. The thicknesses are given in terms of fractional layers, so that this thickness will move as the thickness of the topmost layers change. 
\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in,out}}  & {\em g} & The ocean\textquotesingle{}s grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in}}  & {\em u} & The zonal velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em v} & The meridional velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em tv} & A structure containing pointers to any available thermodynamic fields. Absent fields have N\+U\+LL ptrs. \\
\hline
\mbox{\texttt{ in}}  & {\em forces} & A structure with the driving mechanical forces \\
\hline
\mbox{\texttt{ in,out}}  & {\em visc} & A structure containing vertical viscosities and related fields. \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]}. \\
\hline
 & {\em cs} & The control structure returned by a previous call to set\+\_\+visc\+\_\+init. \\
\hline
\mbox{\texttt{ in}}  & {\em symmetrize} & If present and true, do extra calculations of those values in visc that would be calculated with symmetric memory. \\
\hline
\end{DoxyParams}


Definition at line 1207 of file M\+O\+M\+\_\+set\+\_\+viscosity.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1208   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(inout)} :: G\textcolor{comment}{    !< The ocean's grid structure.}}
\DoxyCodeLine{1209   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure.}}
\DoxyCodeLine{1210   \textcolor{keywordtype}{type}(unit\_scale\_type),   \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{   !< A dimensional unit scaling type}}
\DoxyCodeLine{1211 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{1212                            \textcolor{keywordtype}{intent(in)}    :: u\textcolor{comment}{    !< The zonal velocity [L T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{1213 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{1214                            \textcolor{keywordtype}{intent(in)}    :: v\textcolor{comment}{    !< The meridional velocity [L T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{1215 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))},  \&}
\DoxyCodeLine{1216                            \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{    !< Layer thicknesses [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1217   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),   \textcolor{keywordtype}{intent(in)}    :: tv\textcolor{comment}{   !< A structure containing pointers to any available}}
\DoxyCodeLine{1218 \textcolor{comment}{                                                 !! thermodynamic fields. Absent fields have}}
\DoxyCodeLine{1219 \textcolor{comment}{                                                 !! NULL ptrs.}}
\DoxyCodeLine{1220   \textcolor{keywordtype}{type}(mech\_forcing),      \textcolor{keywordtype}{intent(in)}    :: forces\textcolor{comment}{ !< A structure with the driving mechanical forces}}
\DoxyCodeLine{1221   \textcolor{keywordtype}{type}(vertvisc\_type),     \textcolor{keywordtype}{intent(inout)} :: visc\textcolor{comment}{ !< A structure containing vertical viscosities and}}
\DoxyCodeLine{1222 \textcolor{comment}{                                                 !! related fields.}}
\DoxyCodeLine{1223 \textcolor{keywordtype}{  real},                    \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{   !< Time increment [T \string~> s].}}
\DoxyCodeLine{1224   \textcolor{keywordtype}{type}(set\_visc\_CS),       \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{   !< The control structure returned by a previous}}
\DoxyCodeLine{1225 \textcolor{comment}{                                                 !! call to set\_visc\_init.}}
\DoxyCodeLine{1226   \textcolor{keywordtype}{logical},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: symmetrize\textcolor{comment}{ !< If present and true, do extra calculations}}
\DoxyCodeLine{1227 \textcolor{comment}{                                                 !! of those values in visc that would be}}
\DoxyCodeLine{1228 \textcolor{comment}{                                                 !! calculated with symmetric memory.}}
\DoxyCodeLine{1229 }
\DoxyCodeLine{1230   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{1231 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G))} :: \&}
\DoxyCodeLine{1232     htot, \&     \textcolor{comment}{!   The total depth of the layers being that are within the}}
\DoxyCodeLine{1233                 \textcolor{comment}{! surface mixed layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1234     thtot, \&    \textcolor{comment}{!   The integrated temperature of layers that are within the}}
\DoxyCodeLine{1235                 \textcolor{comment}{! surface mixed layer [H degC \string~> m degC or kg degC m-\/2].}}
\DoxyCodeLine{1236     shtot, \&    \textcolor{comment}{!   The integrated salt of layers that are within the}}
\DoxyCodeLine{1237                 \textcolor{comment}{! surface mixed layer [H ppt \string~> m ppt or kg ppt m-\/2].}}
\DoxyCodeLine{1238     rhtot, \&    \textcolor{comment}{!   The integrated density of layers that are within the surface mixed layer}}
\DoxyCodeLine{1239                 \textcolor{comment}{! [H R \string~> kg m-\/2 or kg2 m-\/5].  Rhtot is only used if no}}
\DoxyCodeLine{1240                 \textcolor{comment}{! equation of state is used.}}
\DoxyCodeLine{1241     uhtot, \&    \textcolor{comment}{!   The depth integrated zonal and meridional velocities within}}
\DoxyCodeLine{1242     vhtot, \&    \textcolor{comment}{! the surface mixed layer [H L T-\/1 \string~> m2 s-\/1 or kg m-\/1 s-\/1].}}
\DoxyCodeLine{1243     idecay\_len\_tke, \& \textcolor{comment}{! The inverse of a turbulence decay length scale [H-\/1 \string~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{1244     dr\_dt, \&    \textcolor{comment}{!   Partial derivative of the density at the base of layer nkml}}
\DoxyCodeLine{1245                 \textcolor{comment}{! (roughly the base of the mixed layer) with temperature [R degC-\/1 \string~> kg m-\/3 degC-\/1].}}
\DoxyCodeLine{1246     dr\_ds, \&    \textcolor{comment}{!   Partial derivative of the density at the base of layer nkml}}
\DoxyCodeLine{1247                 \textcolor{comment}{! (roughly the base of the mixed layer) with salinity [R ppt-\/1 \string~> kg m-\/3 ppt-\/1].}}
\DoxyCodeLine{1248     ustar, \&    \textcolor{comment}{!   The surface friction velocity under ice shelves [Z T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{1249     press, \&    \textcolor{comment}{! The pressure at which dR\_dT and dR\_dS are evaluated [R L2 T-\/2 \string~> Pa].}}
\DoxyCodeLine{1250     t\_eos, \&    \textcolor{comment}{! The potential temperature at which dR\_dT and dR\_dS are evaluated [degC]}}
\DoxyCodeLine{1251     s\_eos       \textcolor{comment}{! The salinity at which dR\_dT and dR\_dS are evaluated [ppt].}}
\DoxyCodeLine{1252 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G))} :: \&}
\DoxyCodeLine{1253     mask\_u      \textcolor{comment}{! A mask that disables any contributions from u points that}}
\DoxyCodeLine{1254                 \textcolor{comment}{! are land or past open boundary conditions [nondim], 0 or 1.}}
\DoxyCodeLine{1255 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G))} :: \&}
\DoxyCodeLine{1256     mask\_v      \textcolor{comment}{! A mask that disables any contributions from v points that}}
\DoxyCodeLine{1257                 \textcolor{comment}{! are land or past open boundary conditions [nondim], 0 or 1.}}
\DoxyCodeLine{1258 \textcolor{keywordtype}{  real} :: h\_at\_vel(SZIB\_(G),SZK\_(G))\textcolor{comment}{! Layer thickness at velocity points,}}
\DoxyCodeLine{1259                 \textcolor{comment}{! using an upwind-\/biased second order accurate estimate based}}
\DoxyCodeLine{1260                 \textcolor{comment}{! on the previous velocity direction [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1261   \textcolor{keywordtype}{integer} :: k\_massive(SZIB\_(G)) \textcolor{comment}{! The k-\/index of the deepest layer yet found}}
\DoxyCodeLine{1262                 \textcolor{comment}{! that has more than h\_tiny thickness and will be in the}}
\DoxyCodeLine{1263                 \textcolor{comment}{! viscous mixed layer.}}
\DoxyCodeLine{1264 \textcolor{keywordtype}{  real} :: Uh2   \textcolor{comment}{! The squared magnitude of the difference between the velocity}}
\DoxyCodeLine{1265                 \textcolor{comment}{! integrated through the mixed layer and the velocity of the}}
\DoxyCodeLine{1266                 \textcolor{comment}{! interior layer layer times the depth of the the mixed layer}}
\DoxyCodeLine{1267                 \textcolor{comment}{! [H2 Z2 T-\/2 \string~> m4 s-\/2 or kg2 m-\/2 s-\/2].}}
\DoxyCodeLine{1268 \textcolor{keywordtype}{  real} :: htot\_vel  \textcolor{comment}{! Sum of the layer thicknesses up to some point [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1269 \textcolor{keywordtype}{  real} :: hwtot     \textcolor{comment}{! Sum of the thicknesses used to calculate}}
\DoxyCodeLine{1270                     \textcolor{comment}{! the near-\/bottom velocity magnitude [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1271 \textcolor{keywordtype}{  real} :: hutot     \textcolor{comment}{! Running sum of thicknesses times the}}
\DoxyCodeLine{1272                     \textcolor{comment}{! velocity magnitudes [H L T-\/1 \string~> m2 s-\/1 or kg m-\/1 s-\/1].}}
\DoxyCodeLine{1273 \textcolor{keywordtype}{  real} :: hweight   \textcolor{comment}{! The thickness of a layer that is within Hbbl}}
\DoxyCodeLine{1274                     \textcolor{comment}{! of the bottom [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1275 \textcolor{keywordtype}{  real} :: tbl\_thick\_Z  \textcolor{comment}{! The thickness of the top boundary layer [Z \string~> m].}}
\DoxyCodeLine{1276 }
\DoxyCodeLine{1277 \textcolor{keywordtype}{  real} :: hlay      \textcolor{comment}{! The layer thickness at velocity points [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1278 \textcolor{keywordtype}{  real} :: I\_2hlay   \textcolor{comment}{! 1 / 2*hlay [H-\/1 \string~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{1279 \textcolor{keywordtype}{  real} :: T\_lay     \textcolor{comment}{! The layer temperature at velocity points [degC].}}
\DoxyCodeLine{1280 \textcolor{keywordtype}{  real} :: S\_lay     \textcolor{comment}{! The layer salinity at velocity points [ppt].}}
\DoxyCodeLine{1281 \textcolor{keywordtype}{  real} :: Rlay      \textcolor{comment}{! The layer potential density at velocity points [R \string~> kg m-\/3].}}
\DoxyCodeLine{1282 \textcolor{keywordtype}{  real} :: Rlb       \textcolor{comment}{! The potential density of the layer below [R \string~> kg m-\/3].}}
\DoxyCodeLine{1283 \textcolor{keywordtype}{  real} :: v\_at\_u    \textcolor{comment}{! The meridonal velocity at a zonal velocity point [L T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{1284 \textcolor{keywordtype}{  real} :: u\_at\_v    \textcolor{comment}{! The zonal velocity at a meridonal velocity point [L T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{1285 \textcolor{keywordtype}{  real} :: gHprime   \textcolor{comment}{! The mixed-\/layer internal gravity wave speed squared, based}}
\DoxyCodeLine{1286                     \textcolor{comment}{! on the mixed layer thickness and density difference across}}
\DoxyCodeLine{1287                     \textcolor{comment}{! the base of the mixed layer [L2 T-\/2 \string~> m2 s-\/2].}}
\DoxyCodeLine{1288 \textcolor{keywordtype}{  real} :: RiBulk    \textcolor{comment}{! The bulk Richardson number below which water is in the}}
\DoxyCodeLine{1289                     \textcolor{comment}{! viscous mixed layer, including reduction for turbulent}}
\DoxyCodeLine{1290                     \textcolor{comment}{! decay. Nondimensional.}}
\DoxyCodeLine{1291 \textcolor{keywordtype}{  real} :: dt\_Rho0   \textcolor{comment}{! The time step divided by the conversion from the layer}}
\DoxyCodeLine{1292                     \textcolor{comment}{! thickness to layer mass [T H Z-\/1 R-\/1 \string~> s m3 kg-\/1 or s].}}
\DoxyCodeLine{1293 \textcolor{keywordtype}{  real} :: g\_H\_Rho0  \textcolor{comment}{!   The gravitational acceleration times the conversion from H to m divided}}
\DoxyCodeLine{1294                     \textcolor{comment}{! by the mean density [L2 T-\/2 H-\/1 R-\/1 \string~> m4 s-\/2 kg-\/1 or m7 s-\/2 kg-\/2].}}
\DoxyCodeLine{1295 \textcolor{keywordtype}{  real} :: ustarsq     \textcolor{comment}{! 400 times the square of ustar, times}}
\DoxyCodeLine{1296                       \textcolor{comment}{! Rho0 divided by G\_Earth and the conversion}}
\DoxyCodeLine{1297                       \textcolor{comment}{! from m to thickness units [H R \string~> kg m-\/2 or kg2 m-\/5].}}
\DoxyCodeLine{1298 \textcolor{keywordtype}{  real} :: cdrag\_sqrt\_Z  \textcolor{comment}{! Square root of the drag coefficient, times a unit conversion}}
\DoxyCodeLine{1299                       \textcolor{comment}{! factor from lateral lengths to vertical depths [Z L-\/1 \string~> 1].}}
\DoxyCodeLine{1300 \textcolor{keywordtype}{  real} :: cdrag\_sqrt  \textcolor{comment}{! Square root of the drag coefficient [nondim].}}
\DoxyCodeLine{1301 \textcolor{keywordtype}{  real} :: oldfn       \textcolor{comment}{! The integrated energy required to}}
\DoxyCodeLine{1302                       \textcolor{comment}{! entrain up to the bottom of the layer,}}
\DoxyCodeLine{1303                       \textcolor{comment}{! divided by G\_Earth [H R \string~> kg m-\/2 or kg2 m-\/5].}}
\DoxyCodeLine{1304 \textcolor{keywordtype}{  real} :: Dfn         \textcolor{comment}{! The increment in oldfn for entraining}}
\DoxyCodeLine{1305                       \textcolor{comment}{! the layer [H R \string~> kg m-\/2 or kg2 m-\/5].}}
\DoxyCodeLine{1306 \textcolor{keywordtype}{  real} :: Dh          \textcolor{comment}{! The increment in layer thickness from}}
\DoxyCodeLine{1307                       \textcolor{comment}{! the present layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1308 \textcolor{keywordtype}{  real} :: U\_bg\_sq   \textcolor{comment}{! The square of an assumed background velocity, for}}
\DoxyCodeLine{1309                     \textcolor{comment}{! calculating the mean magnitude near the top for use in}}
\DoxyCodeLine{1310                     \textcolor{comment}{! the quadratic surface drag [L2 T-\/2 \string~> m2 s-\/2].}}
\DoxyCodeLine{1311 \textcolor{keywordtype}{  real} :: h\_tiny    \textcolor{comment}{! A very small thickness [H \string~> m or kg m-\/2]. Layers that are less than}}
\DoxyCodeLine{1312                     \textcolor{comment}{! h\_tiny can not be the deepest in the viscous mixed layer.}}
\DoxyCodeLine{1313 \textcolor{keywordtype}{  real} :: absf      \textcolor{comment}{! The absolute value of f averaged to velocity points [T-\/1 \string~> s-\/1].}}
\DoxyCodeLine{1314 \textcolor{keywordtype}{  real} :: U\_star    \textcolor{comment}{! The friction velocity at velocity points [Z T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{1315 \textcolor{keywordtype}{  real} :: h\_neglect \textcolor{comment}{! A thickness that is so small it is usually lost}}
\DoxyCodeLine{1316                     \textcolor{comment}{! in roundoff and can be neglected [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1317 \textcolor{keywordtype}{  real} :: Rho0x400\_G \textcolor{comment}{! 400*Rho0/G\_Earth, times unit conversion factors}}
\DoxyCodeLine{1318                      \textcolor{comment}{! [R T2 H Z-\/2 \string~> kg s2 m-\/4 or kg2 s2 m-\/7].}}
\DoxyCodeLine{1319                      \textcolor{comment}{! The 400 is a constant proposed by Killworth and Edwards, 1999.}}
\DoxyCodeLine{1320 \textcolor{keywordtype}{  real} :: ustar1    \textcolor{comment}{! ustar [H T-\/1 \string~> m s-\/1 or kg m-\/2 s-\/1]}}
\DoxyCodeLine{1321 \textcolor{keywordtype}{  real} :: h2f2      \textcolor{comment}{! (h*2*f)\string^2 [H2 T-\/2 \string~> m2 s-\/2 or kg2 m-\/4 s-\/2]}}
\DoxyCodeLine{1322   \textcolor{keywordtype}{logical} :: use\_EOS, do\_any, do\_any\_shelf, do\_i(SZIB\_(G))}
\DoxyCodeLine{1323   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, Isq, Ieq, Jsq, Jeq, nz, K2, nkmb, nkml, n}
\DoxyCodeLine{1324   \textcolor{keywordtype}{type}(ocean\_OBC\_type), \textcolor{keywordtype}{pointer} :: OBC => null()}
\DoxyCodeLine{1325 }
\DoxyCodeLine{1326   is = g\%isc ; ie = g\%iec ; js = g\%jsc ; je = g\%jec ; nz = g\%ke}
\DoxyCodeLine{1327   isq = g\%IscB ; ieq = g\%IecB ; jsq = g\%JscB ; jeq = g\%JecB}
\DoxyCodeLine{1328   nkmb = gv\%nk\_rho\_varies ; nkml = gv\%nkml}
\DoxyCodeLine{1329 }
\DoxyCodeLine{1330   \textcolor{keywordflow}{if} (.not.\textcolor{keyword}{associated}(cs)) \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"MOM\_set\_viscosity(visc\_ML): "}//\&}
\DoxyCodeLine{1331          \textcolor{stringliteral}{"Module must be initialized before it is used."})}
\DoxyCodeLine{1332   \textcolor{keywordflow}{if} (.not.(cs\%dynamic\_viscous\_ML .or. \textcolor{keyword}{associated}(forces\%frac\_shelf\_u) .or. \&}
\DoxyCodeLine{1333             \textcolor{keyword}{associated}(forces\%frac\_shelf\_v)) ) \textcolor{keywordflow}{return}}
\DoxyCodeLine{1334 }
\DoxyCodeLine{1335   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(symmetrize)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (symmetrize) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1336     jsq = js-\/1 ; isq = is-\/1}
\DoxyCodeLine{1337 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1338 }
\DoxyCodeLine{1339   rho0x400\_g = 400.0*(gv\%Rho0/(us\%L\_to\_Z**2 * gv\%g\_Earth)) * gv\%Z\_to\_H}
\DoxyCodeLine{1340   u\_bg\_sq = cs\%drag\_bg\_vel * cs\%drag\_bg\_vel}
\DoxyCodeLine{1341   cdrag\_sqrt = sqrt(cs\%cdrag)}
\DoxyCodeLine{1342   cdrag\_sqrt\_z = us\%L\_to\_Z * sqrt(cs\%cdrag)}
\DoxyCodeLine{1343 }
\DoxyCodeLine{1344   obc => cs\%OBC}
\DoxyCodeLine{1345   use\_eos = \textcolor{keyword}{associated}(tv\%eqn\_of\_state)}
\DoxyCodeLine{1346   dt\_rho0 = dt / gv\%H\_to\_RZ}
\DoxyCodeLine{1347   h\_neglect = gv\%H\_subroundoff}
\DoxyCodeLine{1348   h\_tiny = 2.0*gv\%Angstrom\_H + h\_neglect}
\DoxyCodeLine{1349   g\_h\_rho0 = (gv\%g\_Earth*gv\%H\_to\_Z) / (gv\%Rho0)}
\DoxyCodeLine{1350 }
\DoxyCodeLine{1351   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(forces\%frac\_shelf\_u) .neqv. \textcolor{keyword}{associated}(forces\%frac\_shelf\_v)) \&}
\DoxyCodeLine{1352     \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"set\_viscous\_ML: one of forces\%frac\_shelf\_u and "}//\&}
\DoxyCodeLine{1353                    \textcolor{stringliteral}{"forces\%frac\_shelf\_v is associated, but the other is not."})}
\DoxyCodeLine{1354 }
\DoxyCodeLine{1355   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(forces\%frac\_shelf\_u)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1356     \textcolor{comment}{! This configuration has ice shelves, and the appropriate variables need to be}}
\DoxyCodeLine{1357     \textcolor{comment}{! allocated.  If the arrays have already been allocated, these calls do nothing.}}
\DoxyCodeLine{1358     \textcolor{keyword}{call }safe\_alloc\_ptr(visc\%tauy\_shelf, g\%isd, g\%ied, g\%JsdB, g\%JedB)}
\DoxyCodeLine{1359     \textcolor{keyword}{call }safe\_alloc\_ptr(visc\%tbl\_thick\_shelf\_u, g\%IsdB, g\%IedB, g\%jsd, g\%jed)}
\DoxyCodeLine{1360     \textcolor{keyword}{call }safe\_alloc\_ptr(visc\%tbl\_thick\_shelf\_v, g\%isd, g\%ied, g\%JsdB, g\%JedB)}
\DoxyCodeLine{1361     \textcolor{keyword}{call }safe\_alloc\_ptr(visc\%kv\_tbl\_shelf\_u, g\%IsdB, g\%IedB, g\%jsd, g\%jed)}
\DoxyCodeLine{1362     \textcolor{keyword}{call }safe\_alloc\_ptr(visc\%kv\_tbl\_shelf\_v, g\%isd, g\%ied, g\%JsdB, g\%JedB)}
\DoxyCodeLine{1363     \textcolor{keyword}{call }safe\_alloc\_ptr(visc\%taux\_shelf, g\%IsdB, g\%IedB, g\%jsd, g\%jed)}
\DoxyCodeLine{1364     \textcolor{keyword}{call }safe\_alloc\_ptr(visc\%tauy\_shelf, g\%isd, g\%ied, g\%JsdB, g\%JedB)}
\DoxyCodeLine{1365 }
\DoxyCodeLine{1366     \textcolor{comment}{!  With a linear drag law under shelves, the friction velocity is already known.}}
\DoxyCodeLine{1367 \textcolor{comment}{!    if (CS\%linear\_drag) ustar(:) = cdrag\_sqrt\_Z*CS\%drag\_bg\_vel}}
\DoxyCodeLine{1368 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1369 }
\DoxyCodeLine{1370   \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{1371   \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{do} i=is-\/1,ie+1}
\DoxyCodeLine{1372     mask\_v(i,j) = g\%mask2dCv(i,j)}
\DoxyCodeLine{1373 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1374   \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{1375   \textcolor{keywordflow}{do} j=js-\/1,je+1 ; \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{1376     mask\_u(i,j) = g\%mask2dCu(i,j)}
\DoxyCodeLine{1377 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1378 }
\DoxyCodeLine{1379   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(obc)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} n=1,obc\%number\_of\_segments}
\DoxyCodeLine{1380     \textcolor{comment}{! Now project bottom depths across cell-\/corner points in the OBCs.  The two}}
\DoxyCodeLine{1381     \textcolor{comment}{! projections have to occur in sequence and can not be combined easily.}}
\DoxyCodeLine{1382     \textcolor{keywordflow}{if} (.not. obc\%segment(n)\%on\_pe) cycle}
\DoxyCodeLine{1383     \textcolor{comment}{! Use a one-\/sided projection of bottom depths at OBC points.}}
\DoxyCodeLine{1384     i = obc\%segment(n)\%HI\%IsdB ; j = obc\%segment(n)\%HI\%JsdB}
\DoxyCodeLine{1385     \textcolor{keywordflow}{if} (obc\%segment(n)\%is\_N\_or\_S .and. (j >= js-\/1) .and. (j <= je)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1386       \textcolor{keywordflow}{do} i = max(is-\/1,obc\%segment(n)\%HI\%IsdB), min(ie,obc\%segment(n)\%HI\%IedB)}
\DoxyCodeLine{1387         \textcolor{keywordflow}{if} (obc\%segment(n)\%direction == obc\_direction\_n) mask\_u(i,j+1) = 0.0}
\DoxyCodeLine{1388         \textcolor{keywordflow}{if} (obc\%segment(n)\%direction == obc\_direction\_s) mask\_u(i,j) = 0.0}
\DoxyCodeLine{1389 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1390     \textcolor{keywordflow}{elseif} (obc\%segment(n)\%is\_E\_or\_W .and. (i >= is-\/1) .and. (i <= je)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1391       \textcolor{keywordflow}{do} j = max(js-\/1,obc\%segment(n)\%HI\%JsdB), min(je,obc\%segment(n)\%HI\%JedB)}
\DoxyCodeLine{1392         \textcolor{keywordflow}{if} (obc\%segment(n)\%direction == obc\_direction\_e) mask\_v(i+1,j) = 0.0}
\DoxyCodeLine{1393         \textcolor{keywordflow}{if} (obc\%segment(n)\%direction == obc\_direction\_w) mask\_v(i,j) = 0.0}
\DoxyCodeLine{1394 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1395 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1396 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1397 }
\DoxyCodeLine{1398   \textcolor{comment}{!\$OMP parallel do default(private) shared(u,v,h,tv,forces,visc,dt,G,GV,US,CS,use\_EOS,dt\_Rho0, \&}}
\DoxyCodeLine{1399   \textcolor{comment}{!\$OMP                                     h\_neglect,h\_tiny,g\_H\_Rho0,js,je,OBC,Isq,Ieq,nz,  \&}}
\DoxyCodeLine{1400   \textcolor{comment}{!\$OMP                                     U\_bg\_sq,mask\_v,cdrag\_sqrt,cdrag\_sqrt\_Z,Rho0x400\_G,nkml)}}
\DoxyCodeLine{1401   \textcolor{keywordflow}{do} j=js,je  \textcolor{comment}{! u-\/point loop}}
\DoxyCodeLine{1402     \textcolor{keywordflow}{if} (cs\%dynamic\_viscous\_ML) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1403       do\_any = .false.}
\DoxyCodeLine{1404       \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{1405         htot(i) = 0.0}
\DoxyCodeLine{1406         \textcolor{keywordflow}{if} (g\%mask2dCu(i,j) < 0.5) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1407           do\_i(i) = .false. ; visc\%nkml\_visc\_u(i,j) = nkml}
\DoxyCodeLine{1408         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1409           do\_i(i) = .true. ; do\_any = .true.}
\DoxyCodeLine{1410           k\_massive(i) = nkml}
\DoxyCodeLine{1411           thtot(i) = 0.0 ; shtot(i) = 0.0 ; rhtot(i) = 0.0}
\DoxyCodeLine{1412           uhtot(i) = dt\_rho0 * forces\%taux(i,j)}
\DoxyCodeLine{1413           vhtot(i) = 0.25 * dt\_rho0 * ((forces\%tauy(i,j) + forces\%tauy(i+1,j-\/1)) + \&}
\DoxyCodeLine{1414                                        (forces\%tauy(i,j-\/1) + forces\%tauy(i+1,j)))}
\DoxyCodeLine{1415 }
\DoxyCodeLine{1416           \textcolor{keywordflow}{if} (cs\%omega\_frac >= 1.0) \textcolor{keywordflow}{then} ; absf = 2.0*cs\%omega ; \textcolor{keywordflow}{else}}
\DoxyCodeLine{1417             absf = 0.5*(abs(g\%CoriolisBu(i,j)) + abs(g\%CoriolisBu(i,j-\/1)))}
\DoxyCodeLine{1418             \textcolor{keywordflow}{if} (cs\%omega\_frac > 0.0) \&}
\DoxyCodeLine{1419               absf = sqrt(cs\%omega\_frac*4.0*cs\%omega**2 + (1.0-\/cs\%omega\_frac)*absf**2)}
\DoxyCodeLine{1420 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1421           u\_star = max(cs\%ustar\_min, 0.5 * (forces\%ustar(i,j) + forces\%ustar(i+1,j)))}
\DoxyCodeLine{1422           idecay\_len\_tke(i) = ((absf / u\_star) * cs\%TKE\_decay) * gv\%H\_to\_Z}
\DoxyCodeLine{1423 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1424 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1425 }
\DoxyCodeLine{1426       \textcolor{keywordflow}{if} (do\_any) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{1427         \textcolor{keywordflow}{if} (k > nkml) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1428           do\_any = .false.}
\DoxyCodeLine{1429           \textcolor{keywordflow}{if} (use\_eos .and. (k==nkml+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1430             \textcolor{comment}{! Find dRho/dT and dRho\_dS.}}
\DoxyCodeLine{1431             \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{1432               press(i) = (gv\%H\_to\_RZ*gv\%g\_Earth) * htot(i)}
\DoxyCodeLine{1433               \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%p\_surf)) press(i) = press(i) + 0.5*(tv\%p\_surf(i,j)+tv\%p\_surf(i+1,j))}
\DoxyCodeLine{1434               k2 = max(1,nkml)}
\DoxyCodeLine{1435               i\_2hlay = 1.0 / (h(i,j,k2) + h(i+1,j,k2) + h\_neglect)}
\DoxyCodeLine{1436               t\_eos(i) = (h(i,j,k2)*tv\%T(i,j,k2) + h(i+1,j,k2)*tv\%T(i+1,j,k2)) * i\_2hlay}
\DoxyCodeLine{1437               s\_eos(i) = (h(i,j,k2)*tv\%S(i,j,k2) + h(i+1,j,k2)*tv\%S(i+1,j,k2)) * i\_2hlay}
\DoxyCodeLine{1438 \textcolor{keywordflow}{            enddo}}
\DoxyCodeLine{1439             \textcolor{keyword}{call }calculate\_density\_derivs(t\_eos, s\_eos, press, dr\_dt, dr\_ds, tv\%eqn\_of\_state, \&}
\DoxyCodeLine{1440                                           (/isq-\/g\%IsdB+1,ieq-\/g\%IsdB+1/) )}
\DoxyCodeLine{1441 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1442 }
\DoxyCodeLine{1443           \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1444 }
\DoxyCodeLine{1445             hlay = 0.5*(h(i,j,k) + h(i+1,j,k))}
\DoxyCodeLine{1446             \textcolor{keywordflow}{if} (hlay > h\_tiny) \textcolor{keywordflow}{then} \textcolor{comment}{! Only consider non-\/vanished layers.}}
\DoxyCodeLine{1447               i\_2hlay = 1.0 / (h(i,j,k) + h(i+1,j,k))}
\DoxyCodeLine{1448               v\_at\_u = 0.5 * (h(i,j,k)   * (v(i,j,k) + v(i,j-\/1,k)) + \&}
\DoxyCodeLine{1449                               h(i+1,j,k) * (v(i+1,j,k) + v(i+1,j-\/1,k))) * i\_2hlay}
\DoxyCodeLine{1450               uh2 = ((uhtot(i) -\/ htot(i)*u(i,j,k))**2 + (vhtot(i) -\/ htot(i)*v\_at\_u)**2)}
\DoxyCodeLine{1451 }
\DoxyCodeLine{1452               \textcolor{keywordflow}{if} (use\_eos) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1453                 t\_lay = (h(i,j,k)*tv\%T(i,j,k) + h(i+1,j,k)*tv\%T(i+1,j,k)) * i\_2hlay}
\DoxyCodeLine{1454                 s\_lay = (h(i,j,k)*tv\%S(i,j,k) + h(i+1,j,k)*tv\%S(i+1,j,k)) * i\_2hlay}
\DoxyCodeLine{1455                 ghprime = g\_h\_rho0 * (dr\_dt(i) * (t\_lay*htot(i) -\/ thtot(i)) + \&}
\DoxyCodeLine{1456                                       dr\_ds(i) * (s\_lay*htot(i) -\/ shtot(i)))}
\DoxyCodeLine{1457               \textcolor{keywordflow}{else}}
\DoxyCodeLine{1458                 ghprime = g\_h\_rho0 * (gv\%Rlay(k)*htot(i) -\/ rhtot(i))}
\DoxyCodeLine{1459 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{1460 }
\DoxyCodeLine{1461               \textcolor{keywordflow}{if} (ghprime > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1462                 ribulk = cs\%bulk\_Ri\_ML * exp(-\/htot(i) * idecay\_len\_tke(i))}
\DoxyCodeLine{1463                 \textcolor{keywordflow}{if} (ribulk * uh2 <= (htot(i)**2) * ghprime) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1464                   visc\%nkml\_visc\_u(i,j) = real(k\_massive(i))}
\DoxyCodeLine{1465                   do\_i(i) = .false.}
\DoxyCodeLine{1466                 \textcolor{keywordflow}{elseif} (ribulk * uh2 <= (htot(i) + hlay)**2 * ghprime) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1467                   visc\%nkml\_visc\_u(i,j) = real(k-\/1) + \&}
\DoxyCodeLine{1468                     ( sqrt(ribulk * uh2 / ghprime) -\/ htot(i) ) / hlay}
\DoxyCodeLine{1469                   do\_i(i) = .false.}
\DoxyCodeLine{1470 \textcolor{keywordflow}{                endif}}
\DoxyCodeLine{1471 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{1472               k\_massive(i) = k}
\DoxyCodeLine{1473 \textcolor{keywordflow}{            endif} \textcolor{comment}{! hlay > h\_tiny}}
\DoxyCodeLine{1474 }
\DoxyCodeLine{1475             \textcolor{keywordflow}{if} (do\_i(i)) do\_any = .true.}
\DoxyCodeLine{1476 \textcolor{keywordflow}{          endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1477 }
\DoxyCodeLine{1478           \textcolor{keywordflow}{if} (.not.do\_any) \textcolor{keywordflow}{exit} \textcolor{comment}{! All columns are done.}}
\DoxyCodeLine{1479 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1480 }
\DoxyCodeLine{1481         \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1482           htot(i) = htot(i) + 0.5 * (h(i,j,k) + h(i+1,j,k))}
\DoxyCodeLine{1483           uhtot(i) = uhtot(i) + 0.5 * (h(i,j,k) + h(i+1,j,k)) * u(i,j,k)}
\DoxyCodeLine{1484           vhtot(i) = vhtot(i) + 0.25 * (h(i,j,k) * (v(i,j,k) + v(i,j-\/1,k)) + \&}
\DoxyCodeLine{1485                                         h(i+1,j,k) * (v(i+1,j,k) + v(i+1,j-\/1,k)))}
\DoxyCodeLine{1486           \textcolor{keywordflow}{if} (use\_eos) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1487             thtot(i) = thtot(i) + 0.5 * (h(i,j,k)*tv\%T(i,j,k) + h(i+1,j,k)*tv\%T(i+1,j,k))}
\DoxyCodeLine{1488             shtot(i) = shtot(i) + 0.5 * (h(i,j,k)*tv\%S(i,j,k) + h(i+1,j,k)*tv\%S(i+1,j,k))}
\DoxyCodeLine{1489           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1490             rhtot(i) = rhtot(i) + 0.5 * (h(i,j,k) + h(i+1,j,k)) * gv\%Rlay(k)}
\DoxyCodeLine{1491 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1492 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1493 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1494 }
\DoxyCodeLine{1495       \textcolor{keywordflow}{if} (do\_any) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1496         visc\%nkml\_visc\_u(i,j) = k\_massive(i)}
\DoxyCodeLine{1497 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1498 \textcolor{keywordflow}{    endif} \textcolor{comment}{! dynamic\_viscous\_ML}}
\DoxyCodeLine{1499 }
\DoxyCodeLine{1500     do\_any\_shelf = .false.}
\DoxyCodeLine{1501     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(forces\%frac\_shelf\_u)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1502       \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{1503         \textcolor{keywordflow}{if} (forces\%frac\_shelf\_u(i,j)*g\%mask2dCu(i,j) == 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1504           do\_i(i) = .false.}
\DoxyCodeLine{1505           visc\%tbl\_thick\_shelf\_u(i,j) = 0.0 ; visc\%kv\_tbl\_shelf\_u(i,j) = 0.0}
\DoxyCodeLine{1506         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1507           do\_i(i) = .true. ; do\_any\_shelf = .true.}
\DoxyCodeLine{1508 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1509 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1510 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1511 }
\DoxyCodeLine{1512     \textcolor{keywordflow}{if} (do\_any\_shelf) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1513       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1514         \textcolor{keywordflow}{if} (u(i,j,k) *(h(i+1,j,k) -\/ h(i,j,k)) >= 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1515           h\_at\_vel(i,k) = 2.0*h(i,j,k)*h(i+1,j,k) / \&}
\DoxyCodeLine{1516                           (h(i,j,k) + h(i+1,j,k) + h\_neglect)}
\DoxyCodeLine{1517         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1518           h\_at\_vel(i,k) =  0.5 * (h(i,j,k) + h(i+1,j,k))}
\DoxyCodeLine{1519 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1520       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1521         h\_at\_vel(i,k) = 0.0 ; ustar(i) = 0.0}
\DoxyCodeLine{1522 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1523 }
\DoxyCodeLine{1524       \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1525         htot\_vel = 0.0 ; hwtot = 0.0 ; hutot = 0.0}
\DoxyCodeLine{1526         thtot(i) = 0.0 ; shtot(i) = 0.0}
\DoxyCodeLine{1527         \textcolor{keywordflow}{if} (use\_eos .or. .not.cs\%linear\_drag) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{1528           \textcolor{keywordflow}{if} (htot\_vel>=cs\%Htbl\_shelf) \textcolor{keywordflow}{exit} \textcolor{comment}{! terminate the k loop}}
\DoxyCodeLine{1529           hweight = min(cs\%Htbl\_shelf -\/ htot\_vel, h\_at\_vel(i,k))}
\DoxyCodeLine{1530           \textcolor{keywordflow}{if} (hweight <= 1.5*gv\%Angstrom\_H + h\_neglect) cycle}
\DoxyCodeLine{1531 }
\DoxyCodeLine{1532           htot\_vel  = htot\_vel + h\_at\_vel(i,k)}
\DoxyCodeLine{1533           hwtot = hwtot + hweight}
\DoxyCodeLine{1534 }
\DoxyCodeLine{1535           \textcolor{keywordflow}{if} (.not.cs\%linear\_drag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1536             v\_at\_u = set\_v\_at\_u(v, h, g, i, j, k, mask\_v, obc)}
\DoxyCodeLine{1537             hutot = hutot + hweight * sqrt(u(i,j,k)**2 + \&}
\DoxyCodeLine{1538                                            v\_at\_u**2 + u\_bg\_sq)}
\DoxyCodeLine{1539 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1540           \textcolor{keywordflow}{if} (use\_eos) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1541             thtot(i) = thtot(i) + hweight * 0.5 * (tv\%T(i,j,k) + tv\%T(i+1,j,k))}
\DoxyCodeLine{1542             shtot(i) = shtot(i) + hweight * 0.5 * (tv\%S(i,j,k) + tv\%S(i+1,j,k))}
\DoxyCodeLine{1543 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1544 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1545 }
\DoxyCodeLine{1546         \textcolor{keywordflow}{if} ((.not.cs\%linear\_drag) .and. (hwtot > 0.0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1547           ustar(i) = cdrag\_sqrt\_z * hutot/hwtot}
\DoxyCodeLine{1548         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1549           ustar(i) = cdrag\_sqrt\_z * cs\%drag\_bg\_vel}
\DoxyCodeLine{1550 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1551 }
\DoxyCodeLine{1552         \textcolor{keywordflow}{if} (use\_eos) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (hwtot > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1553           t\_eos(i) = thtot(i)/hwtot ; s\_eos(i) = shtot(i)/hwtot}
\DoxyCodeLine{1554         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1555           t\_eos(i) = 0.0 ; s\_eos(i) = 0.0}
\DoxyCodeLine{1556 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1557 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! I-\/loop}}
\DoxyCodeLine{1558 }
\DoxyCodeLine{1559       \textcolor{keywordflow}{if} (use\_eos) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1560         \textcolor{keyword}{call }calculate\_density\_derivs(t\_eos, s\_eos, forces\%p\_surf(:,j), dr\_dt, dr\_ds, \&}
\DoxyCodeLine{1561                                       tv\%eqn\_of\_state, (/isq-\/g\%IsdB+1,ieq-\/g\%IsdB+1/) )}
\DoxyCodeLine{1562 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1563 }
\DoxyCodeLine{1564       \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1565   \textcolor{comment}{!  The 400.0 in this expression is the square of a constant proposed}}
\DoxyCodeLine{1566   \textcolor{comment}{!  by Killworth and Edwards, 1999, in equation (2.20).}}
\DoxyCodeLine{1567         ustarsq = rho0x400\_g * ustar(i)**2}
\DoxyCodeLine{1568         htot(i) = 0.0}
\DoxyCodeLine{1569         \textcolor{keywordflow}{if} (use\_eos) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1570           thtot(i) = 0.0 ; shtot(i) = 0.0}
\DoxyCodeLine{1571           \textcolor{keywordflow}{do} k=1,nz-\/1}
\DoxyCodeLine{1572             \textcolor{keywordflow}{if} (h\_at\_vel(i,k) <= 0.0) cycle}
\DoxyCodeLine{1573             t\_lay = 0.5 * (tv\%T(i,j,k) + tv\%T(i+1,j,k))}
\DoxyCodeLine{1574             s\_lay = 0.5 * (tv\%S(i,j,k) + tv\%S(i+1,j,k))}
\DoxyCodeLine{1575             oldfn = dr\_dt(i)*(t\_lay*htot(i) -\/ thtot(i)) + dr\_ds(i)*(s\_lay*htot(i) -\/ shtot(i))}
\DoxyCodeLine{1576             \textcolor{keywordflow}{if} (oldfn >= ustarsq) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{1577 }
\DoxyCodeLine{1578             dfn = (dr\_dt(i)*(0.5*(tv\%T(i,j,k+1)+tv\%T(i+1,j,k+1)) -\/ t\_lay) + \&}
\DoxyCodeLine{1579                    dr\_ds(i)*(0.5*(tv\%S(i,j,k+1)+tv\%S(i+1,j,k+1)) -\/ s\_lay)) * \&}
\DoxyCodeLine{1580                   (h\_at\_vel(i,k)+htot(i))}
\DoxyCodeLine{1581             \textcolor{keywordflow}{if} ((oldfn + dfn) <= ustarsq) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1582               dh = h\_at\_vel(i,k)}
\DoxyCodeLine{1583             \textcolor{keywordflow}{else}}
\DoxyCodeLine{1584               dh = h\_at\_vel(i,k) * sqrt((ustarsq-\/oldfn) / (dfn))}
\DoxyCodeLine{1585 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{1586 }
\DoxyCodeLine{1587             htot(i) = htot(i) + dh}
\DoxyCodeLine{1588             thtot(i) = thtot(i) + t\_lay*dh ; shtot(i) = shtot(i) + s\_lay*dh}
\DoxyCodeLine{1589 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{1590           \textcolor{keywordflow}{if} ((oldfn < ustarsq) .and. (h\_at\_vel(i,nz) > 0.0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1591             t\_lay = 0.5*(tv\%T(i,j,nz) + tv\%T(i+1,j,nz))}
\DoxyCodeLine{1592             s\_lay = 0.5*(tv\%S(i,j,nz) + tv\%S(i+1,j,nz))}
\DoxyCodeLine{1593             \textcolor{keywordflow}{if} (dr\_dt(i)*(t\_lay*htot(i) -\/ thtot(i)) + \&}
\DoxyCodeLine{1594                 dr\_ds(i)*(s\_lay*htot(i) -\/ shtot(i)) < ustarsq) \&}
\DoxyCodeLine{1595               htot(i) = htot(i) + h\_at\_vel(i,nz)}
\DoxyCodeLine{1596 \textcolor{keywordflow}{          endif} \textcolor{comment}{! Examination of layer nz.}}
\DoxyCodeLine{1597         \textcolor{keywordflow}{else}  \textcolor{comment}{! Use Rlay as the density variable.}}
\DoxyCodeLine{1598           rhtot = 0.0}
\DoxyCodeLine{1599           \textcolor{keywordflow}{do} k=1,nz-\/1}
\DoxyCodeLine{1600             rlay = gv\%Rlay(k) ; rlb = gv\%Rlay(k+1)}
\DoxyCodeLine{1601 }
\DoxyCodeLine{1602             oldfn = rlay*htot(i) -\/ rhtot(i)}
\DoxyCodeLine{1603             \textcolor{keywordflow}{if} (oldfn >= ustarsq) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{1604 }
\DoxyCodeLine{1605             dfn = (rlb -\/ rlay)*(h\_at\_vel(i,k)+htot(i))}
\DoxyCodeLine{1606             \textcolor{keywordflow}{if} ((oldfn + dfn) <= ustarsq) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1607               dh = h\_at\_vel(i,k)}
\DoxyCodeLine{1608             \textcolor{keywordflow}{else}}
\DoxyCodeLine{1609               dh = h\_at\_vel(i,k) * sqrt((ustarsq-\/oldfn) / (dfn))}
\DoxyCodeLine{1610 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{1611 }
\DoxyCodeLine{1612             htot(i) = htot(i) + dh}
\DoxyCodeLine{1613             rhtot(i) = rhtot(i) + rlay*dh}
\DoxyCodeLine{1614 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{1615           \textcolor{keywordflow}{if} (gv\%Rlay(nz)*htot(i) -\/ rhtot(i) < ustarsq) \&}
\DoxyCodeLine{1616             htot(i) = htot(i) + h\_at\_vel(i,nz)}
\DoxyCodeLine{1617 \textcolor{keywordflow}{        endif} \textcolor{comment}{! use\_EOS}}
\DoxyCodeLine{1618 }
\DoxyCodeLine{1619        \textcolor{comment}{!visc\%tbl\_thick\_shelf\_u(I,j) = GV\%H\_to\_Z * max(CS\%Htbl\_shelf\_min, \&}}
\DoxyCodeLine{1620        \textcolor{comment}{!    htot(I) / (0.5 + sqrt(0.25 + \&}}
\DoxyCodeLine{1621        \textcolor{comment}{!                 (htot(i)*(G\%CoriolisBu(I,J-\/1)+G\%CoriolisBu(I,J)))**2 / \&}}
\DoxyCodeLine{1622        \textcolor{comment}{!                 (ustar(i)*GV\%Z\_to\_H)**2 )) )}}
\DoxyCodeLine{1623         ustar1 = ustar(i)*gv\%Z\_to\_H}
\DoxyCodeLine{1624         h2f2 = (htot(i)*(g\%CoriolisBu(i,j-\/1)+g\%CoriolisBu(i,j)) + h\_neglect*cs\%omega)**2}
\DoxyCodeLine{1625         tbl\_thick\_z = gv\%H\_to\_Z * max(cs\%Htbl\_shelf\_min, \&}
\DoxyCodeLine{1626             ( htot(i)*ustar1 ) / ( 0.5*ustar1 + sqrt((0.5*ustar1)**2 + h2f2 ) ) )}
\DoxyCodeLine{1627         visc\%tbl\_thick\_shelf\_u(i,j) = tbl\_thick\_z}
\DoxyCodeLine{1628         visc\%Kv\_tbl\_shelf\_u(i,j) = max(cs\%Kv\_TBL\_min, cdrag\_sqrt*ustar(i)*tbl\_thick\_z)}
\DoxyCodeLine{1629 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! I-\/loop}}
\DoxyCodeLine{1630 \textcolor{keywordflow}{    endif} \textcolor{comment}{! do\_any\_shelf}}
\DoxyCodeLine{1631 }
\DoxyCodeLine{1632 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! j-\/loop at u-\/points}}
\DoxyCodeLine{1633 }
\DoxyCodeLine{1634   \textcolor{comment}{!\$OMP parallel do default(private) shared(u,v,h,tv,forces,visc,dt,G,GV,US,CS,use\_EOS,dt\_Rho0, \&}}
\DoxyCodeLine{1635   \textcolor{comment}{!\$OMP                                     h\_neglect,h\_tiny,g\_H\_Rho0,is,ie,OBC,Jsq,Jeq,nz, \&}}
\DoxyCodeLine{1636   \textcolor{comment}{!\$OMP                                     U\_bg\_sq,cdrag\_sqrt,cdrag\_sqrt\_Z,Rho0x400\_G,nkml,mask\_u)}}
\DoxyCodeLine{1637   \textcolor{keywordflow}{do} j=jsq,jeq  \textcolor{comment}{! v-\/point loop}}
\DoxyCodeLine{1638     \textcolor{keywordflow}{if} (cs\%dynamic\_viscous\_ML) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1639       do\_any = .false.}
\DoxyCodeLine{1640       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1641         htot(i) = 0.0}
\DoxyCodeLine{1642         \textcolor{keywordflow}{if} (g\%mask2dCv(i,j) < 0.5) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1643           do\_i(i) = .false. ; visc\%nkml\_visc\_v(i,j) = nkml}
\DoxyCodeLine{1644         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1645           do\_i(i) = .true. ; do\_any = .true.}
\DoxyCodeLine{1646           k\_massive(i) = nkml}
\DoxyCodeLine{1647           thtot(i) = 0.0 ; shtot(i) = 0.0 ; rhtot(i) = 0.0}
\DoxyCodeLine{1648           vhtot(i) = dt\_rho0 * forces\%tauy(i,j)}
\DoxyCodeLine{1649           uhtot(i) = 0.25 * dt\_rho0 * ((forces\%taux(i,j) + forces\%taux(i-\/1,j+1)) + \&}
\DoxyCodeLine{1650                                        (forces\%taux(i-\/1,j) + forces\%taux(i,j+1)))}
\DoxyCodeLine{1651 }
\DoxyCodeLine{1652          \textcolor{keywordflow}{if} (cs\%omega\_frac >= 1.0) \textcolor{keywordflow}{then} ; absf = 2.0*cs\%omega ; \textcolor{keywordflow}{else}}
\DoxyCodeLine{1653            absf = 0.5*(abs(g\%CoriolisBu(i-\/1,j)) + abs(g\%CoriolisBu(i,j)))}
\DoxyCodeLine{1654            \textcolor{keywordflow}{if} (cs\%omega\_frac > 0.0) \&}
\DoxyCodeLine{1655              absf = sqrt(cs\%omega\_frac*4.0*cs\%omega**2 + (1.0-\/cs\%omega\_frac)*absf**2)}
\DoxyCodeLine{1656 \textcolor{keywordflow}{         endif}}
\DoxyCodeLine{1657 }
\DoxyCodeLine{1658          u\_star = max(cs\%ustar\_min, 0.5 * (forces\%ustar(i,j) + forces\%ustar(i,j+1)))}
\DoxyCodeLine{1659          idecay\_len\_tke(i) = ((absf / u\_star) * cs\%TKE\_decay) * gv\%H\_to\_Z}
\DoxyCodeLine{1660 }
\DoxyCodeLine{1661 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1662 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1663 }
\DoxyCodeLine{1664       \textcolor{keywordflow}{if} (do\_any) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{1665         \textcolor{keywordflow}{if} (k > nkml) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1666           do\_any = .false.}
\DoxyCodeLine{1667           \textcolor{keywordflow}{if} (use\_eos .and. (k==nkml+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1668             \textcolor{comment}{! Find dRho/dT and dRho\_dS.}}
\DoxyCodeLine{1669             \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1670               press(i) = (gv\%H\_to\_RZ * gv\%g\_Earth) * htot(i)}
\DoxyCodeLine{1671               \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%p\_surf)) press(i) = press(i) + 0.5*(tv\%p\_surf(i,j)+tv\%p\_surf(i,j+1))}
\DoxyCodeLine{1672               k2 = max(1,nkml)}
\DoxyCodeLine{1673               i\_2hlay = 1.0 / (h(i,j,k2) + h(i,j+1,k2) + h\_neglect)}
\DoxyCodeLine{1674               t\_eos(i) = (h(i,j,k2)*tv\%T(i,j,k2) + h(i,j+1,k2)*tv\%T(i,j+1,k2)) * i\_2hlay}
\DoxyCodeLine{1675               s\_eos(i) = (h(i,j,k2)*tv\%S(i,j,k2) + h(i,j+1,k2)*tv\%S(i,j+1,k2)) * i\_2hlay}
\DoxyCodeLine{1676 \textcolor{keywordflow}{            enddo}}
\DoxyCodeLine{1677             \textcolor{keyword}{call }calculate\_density\_derivs(t\_eos, s\_eos, press, dr\_dt, dr\_ds, \&}
\DoxyCodeLine{1678                                           tv\%eqn\_of\_state, (/is-\/g\%IsdB+1,ie-\/g\%IsdB+1/) )}
\DoxyCodeLine{1679 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1680 }
\DoxyCodeLine{1681           \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1682 }
\DoxyCodeLine{1683             hlay = 0.5*(h(i,j,k) + h(i,j+1,k))}
\DoxyCodeLine{1684             \textcolor{keywordflow}{if} (hlay > h\_tiny) \textcolor{keywordflow}{then} \textcolor{comment}{! Only consider non-\/vanished layers.}}
\DoxyCodeLine{1685               i\_2hlay = 1.0 / (h(i,j,k) + h(i,j+1,k))}
\DoxyCodeLine{1686               u\_at\_v = 0.5 * (h(i,j,k)   * (u(i-\/1,j,k)   + u(i,j,k)) + \&}
\DoxyCodeLine{1687                               h(i,j+1,k) * (u(i-\/1,j+1,k) + u(i,j+1,k))) * i\_2hlay}
\DoxyCodeLine{1688               uh2 = ((uhtot(i) -\/ htot(i)*u\_at\_v)**2 + (vhtot(i) -\/ htot(i)*v(i,j,k))**2)}
\DoxyCodeLine{1689 }
\DoxyCodeLine{1690               \textcolor{keywordflow}{if} (use\_eos) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1691                 t\_lay = (h(i,j,k)*tv\%T(i,j,k) + h(i,j+1,k)*tv\%T(i,j+1,k)) * i\_2hlay}
\DoxyCodeLine{1692                 s\_lay = (h(i,j,k)*tv\%S(i,j,k) + h(i,j+1,k)*tv\%S(i,j+1,k)) * i\_2hlay}
\DoxyCodeLine{1693                 ghprime = g\_h\_rho0 * (dr\_dt(i) * (t\_lay*htot(i) -\/ thtot(i)) + \&}
\DoxyCodeLine{1694                                       dr\_ds(i) * (s\_lay*htot(i) -\/ shtot(i)))}
\DoxyCodeLine{1695               \textcolor{keywordflow}{else}}
\DoxyCodeLine{1696                 ghprime = g\_h\_rho0 * (gv\%Rlay(k)*htot(i) -\/ rhtot(i))}
\DoxyCodeLine{1697 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{1698 }
\DoxyCodeLine{1699               \textcolor{keywordflow}{if} (ghprime > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1700                 ribulk = cs\%bulk\_Ri\_ML * exp(-\/htot(i) * idecay\_len\_tke(i))}
\DoxyCodeLine{1701                 \textcolor{keywordflow}{if} (ribulk * uh2 <= htot(i)**2 * ghprime) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1702                   visc\%nkml\_visc\_v(i,j) = real(k\_massive(i))}
\DoxyCodeLine{1703                   do\_i(i) = .false.}
\DoxyCodeLine{1704                 \textcolor{keywordflow}{elseif} (ribulk * uh2 <= (htot(i) + hlay)**2 * ghprime) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1705                   visc\%nkml\_visc\_v(i,j) = real(k-\/1) + \&}
\DoxyCodeLine{1706                     ( sqrt(ribulk * uh2 / ghprime) -\/ htot(i) ) / hlay}
\DoxyCodeLine{1707                   do\_i(i) = .false.}
\DoxyCodeLine{1708 \textcolor{keywordflow}{                endif}}
\DoxyCodeLine{1709 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{1710               k\_massive(i) = k}
\DoxyCodeLine{1711 \textcolor{keywordflow}{            endif} \textcolor{comment}{! hlay > h\_tiny}}
\DoxyCodeLine{1712 }
\DoxyCodeLine{1713             \textcolor{keywordflow}{if} (do\_i(i)) do\_any = .true.}
\DoxyCodeLine{1714 \textcolor{keywordflow}{          endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1715 }
\DoxyCodeLine{1716           \textcolor{keywordflow}{if} (.not.do\_any) \textcolor{keywordflow}{exit} \textcolor{comment}{! All columns are done.}}
\DoxyCodeLine{1717 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1718 }
\DoxyCodeLine{1719         \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1720           htot(i) = htot(i) + 0.5 * (h(i,j,k) + h(i,j+1,k))}
\DoxyCodeLine{1721           vhtot(i) = vhtot(i) + 0.5 * (h(i,j,k) + h(i,j+1,k)) * v(i,j,k)}
\DoxyCodeLine{1722           uhtot(i) = uhtot(i) + 0.25 * (h(i,j,k) * (u(i-\/1,j,k) + u(i,j,k)) + \&}
\DoxyCodeLine{1723                                         h(i,j+1,k) * (u(i-\/1,j+1,k) + u(i,j+1,k)))}
\DoxyCodeLine{1724           \textcolor{keywordflow}{if} (use\_eos) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1725             thtot(i) = thtot(i) + 0.5 * (h(i,j,k)*tv\%T(i,j,k) + h(i,j+1,k)*tv\%T(i,j+1,k))}
\DoxyCodeLine{1726             shtot(i) = shtot(i) + 0.5 * (h(i,j,k)*tv\%S(i,j,k) + h(i,j+1,k)*tv\%S(i,j+1,k))}
\DoxyCodeLine{1727           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1728             rhtot(i) = rhtot(i) + 0.5 * (h(i,j,k) + h(i,j+1,k)) * gv\%Rlay(k)}
\DoxyCodeLine{1729 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1730 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1731 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1732 }
\DoxyCodeLine{1733       \textcolor{keywordflow}{if} (do\_any) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1734         visc\%nkml\_visc\_v(i,j) = k\_massive(i)}
\DoxyCodeLine{1735 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1736 \textcolor{keywordflow}{    endif} \textcolor{comment}{! dynamic\_viscous\_ML}}
\DoxyCodeLine{1737 }
\DoxyCodeLine{1738     do\_any\_shelf = .false.}
\DoxyCodeLine{1739     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(forces\%frac\_shelf\_v)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1740       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1741         \textcolor{keywordflow}{if} (forces\%frac\_shelf\_v(i,j)*g\%mask2dCv(i,j) == 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1742           do\_i(i) = .false.}
\DoxyCodeLine{1743           visc\%tbl\_thick\_shelf\_v(i,j) = 0.0 ; visc\%kv\_tbl\_shelf\_v(i,j) = 0.0}
\DoxyCodeLine{1744         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1745           do\_i(i) = .true. ; do\_any\_shelf = .true.}
\DoxyCodeLine{1746 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1747 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1748 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1749 }
\DoxyCodeLine{1750     \textcolor{keywordflow}{if} (do\_any\_shelf) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1751       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1752         \textcolor{keywordflow}{if} (v(i,j,k) * (h(i,j+1,k) -\/ h(i,j,k)) >= 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1753           h\_at\_vel(i,k) = 2.0*h(i,j,k)*h(i,j+1,k) / \&}
\DoxyCodeLine{1754                           (h(i,j,k) + h(i,j+1,k) + h\_neglect)}
\DoxyCodeLine{1755         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1756           h\_at\_vel(i,k) =  0.5 * (h(i,j,k) + h(i,j+1,k))}
\DoxyCodeLine{1757 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1758       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1759         h\_at\_vel(i,k) = 0.0 ; ustar(i) = 0.0}
\DoxyCodeLine{1760 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1761 }
\DoxyCodeLine{1762       \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1763         htot\_vel = 0.0 ; hwtot = 0.0 ; hutot = 0.0}
\DoxyCodeLine{1764         thtot(i) = 0.0 ; shtot(i) = 0.0}
\DoxyCodeLine{1765         \textcolor{keywordflow}{if} (use\_eos .or. .not.cs\%linear\_drag) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{1766           \textcolor{keywordflow}{if} (htot\_vel>=cs\%Htbl\_shelf) \textcolor{keywordflow}{exit} \textcolor{comment}{! terminate the k loop}}
\DoxyCodeLine{1767           hweight = min(cs\%Htbl\_shelf -\/ htot\_vel, h\_at\_vel(i,k))}
\DoxyCodeLine{1768           \textcolor{keywordflow}{if} (hweight <= 1.5*gv\%Angstrom\_H + h\_neglect) cycle}
\DoxyCodeLine{1769 }
\DoxyCodeLine{1770           htot\_vel  = htot\_vel + h\_at\_vel(i,k)}
\DoxyCodeLine{1771           hwtot = hwtot + hweight}
\DoxyCodeLine{1772 }
\DoxyCodeLine{1773           \textcolor{keywordflow}{if} (.not.cs\%linear\_drag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1774             u\_at\_v = set\_u\_at\_v(u, h, g, i, j, k, mask\_u, obc)}
\DoxyCodeLine{1775             hutot = hutot + hweight * sqrt(v(i,j,k)**2 + \&}
\DoxyCodeLine{1776                                            u\_at\_v**2 + u\_bg\_sq)}
\DoxyCodeLine{1777 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1778           \textcolor{keywordflow}{if} (use\_eos) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1779             thtot(i) = thtot(i) + hweight * 0.5 * (tv\%T(i,j,k) + tv\%T(i,j+1,k))}
\DoxyCodeLine{1780             shtot(i) = shtot(i) + hweight * 0.5 * (tv\%S(i,j,k) + tv\%S(i,j+1,k))}
\DoxyCodeLine{1781 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1782 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1783 }
\DoxyCodeLine{1784         \textcolor{keywordflow}{if} (.not.cs\%linear\_drag) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (hwtot > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1785           ustar(i) = cdrag\_sqrt\_z * hutot/hwtot}
\DoxyCodeLine{1786         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1787           ustar(i) = cdrag\_sqrt\_z * cs\%drag\_bg\_vel}
\DoxyCodeLine{1788 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1789 }
\DoxyCodeLine{1790         \textcolor{keywordflow}{if} (use\_eos) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (hwtot > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1791           t\_eos(i) = thtot(i)/hwtot ; s\_eos(i) = shtot(i)/hwtot}
\DoxyCodeLine{1792         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1793           t\_eos(i) = 0.0 ; s\_eos(i) = 0.0}
\DoxyCodeLine{1794 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1795 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! I-\/loop}}
\DoxyCodeLine{1796 }
\DoxyCodeLine{1797       \textcolor{keywordflow}{if} (use\_eos) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1798         \textcolor{keyword}{call }calculate\_density\_derivs(t\_eos, s\_eos, forces\%p\_surf(:,j), dr\_dt, dr\_ds, \&}
\DoxyCodeLine{1799                                       tv\%eqn\_of\_state, (/is-\/g\%IsdB+1,ie-\/g\%IsdB+1/) )}
\DoxyCodeLine{1800 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1801 }
\DoxyCodeLine{1802       \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1803   \textcolor{comment}{!  The 400.0 in this expression is the square of a constant proposed}}
\DoxyCodeLine{1804   \textcolor{comment}{!  by Killworth and Edwards, 1999, in equation (2.20).}}
\DoxyCodeLine{1805         ustarsq = rho0x400\_g * ustar(i)**2}
\DoxyCodeLine{1806         htot(i) = 0.0}
\DoxyCodeLine{1807         \textcolor{keywordflow}{if} (use\_eos) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1808           thtot(i) = 0.0 ; shtot(i) = 0.0}
\DoxyCodeLine{1809           \textcolor{keywordflow}{do} k=1,nz-\/1}
\DoxyCodeLine{1810             \textcolor{keywordflow}{if} (h\_at\_vel(i,k) <= 0.0) cycle}
\DoxyCodeLine{1811             t\_lay = 0.5 * (tv\%T(i,j,k) + tv\%T(i,j+1,k))}
\DoxyCodeLine{1812             s\_lay = 0.5 * (tv\%S(i,j,k) + tv\%S(i,j+1,k))}
\DoxyCodeLine{1813             oldfn = dr\_dt(i)*(t\_lay*htot(i) -\/ thtot(i)) + dr\_ds(i)*(s\_lay*htot(i) -\/ shtot(i))}
\DoxyCodeLine{1814             \textcolor{keywordflow}{if} (oldfn >= ustarsq) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{1815 }
\DoxyCodeLine{1816             dfn = (dr\_dt(i)*(0.5*(tv\%T(i,j,k+1)+tv\%T(i,j+1,k+1)) -\/ t\_lay) + \&}
\DoxyCodeLine{1817                    dr\_ds(i)*(0.5*(tv\%S(i,j,k+1)+tv\%S(i,j+1,k+1)) -\/ s\_lay)) * \&}
\DoxyCodeLine{1818                   (h\_at\_vel(i,k)+htot(i))}
\DoxyCodeLine{1819             \textcolor{keywordflow}{if} ((oldfn + dfn) <= ustarsq) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1820               dh = h\_at\_vel(i,k)}
\DoxyCodeLine{1821             \textcolor{keywordflow}{else}}
\DoxyCodeLine{1822               dh = h\_at\_vel(i,k) * sqrt((ustarsq-\/oldfn) / (dfn))}
\DoxyCodeLine{1823 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{1824 }
\DoxyCodeLine{1825             htot(i) = htot(i) + dh}
\DoxyCodeLine{1826             thtot(i) = thtot(i) + t\_lay*dh ; shtot(i) = shtot(i) + s\_lay*dh}
\DoxyCodeLine{1827 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{1828           \textcolor{keywordflow}{if} ((oldfn < ustarsq) .and. (h\_at\_vel(i,nz) > 0.0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1829             t\_lay = 0.5*(tv\%T(i,j,nz) + tv\%T(i,j+1,nz))}
\DoxyCodeLine{1830             s\_lay = 0.5*(tv\%S(i,j,nz) + tv\%S(i,j+1,nz))}
\DoxyCodeLine{1831             \textcolor{keywordflow}{if} (dr\_dt(i)*(t\_lay*htot(i) -\/ thtot(i)) + \&}
\DoxyCodeLine{1832                 dr\_ds(i)*(s\_lay*htot(i) -\/ shtot(i)) < ustarsq) \&}
\DoxyCodeLine{1833               htot(i) = htot(i) + h\_at\_vel(i,nz)}
\DoxyCodeLine{1834 \textcolor{keywordflow}{          endif} \textcolor{comment}{! Examination of layer nz.}}
\DoxyCodeLine{1835         \textcolor{keywordflow}{else}  \textcolor{comment}{! Use Rlay as the density variable.}}
\DoxyCodeLine{1836           rhtot = 0.0}
\DoxyCodeLine{1837           \textcolor{keywordflow}{do} k=1,nz-\/1}
\DoxyCodeLine{1838             rlay = gv\%Rlay(k) ; rlb = gv\%Rlay(k+1)}
\DoxyCodeLine{1839 }
\DoxyCodeLine{1840             oldfn = rlay*htot(i) -\/ rhtot(i)}
\DoxyCodeLine{1841             \textcolor{keywordflow}{if} (oldfn >= ustarsq) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{1842 }
\DoxyCodeLine{1843             dfn = (rlb -\/ rlay)*(h\_at\_vel(i,k)+htot(i))}
\DoxyCodeLine{1844             \textcolor{keywordflow}{if} ((oldfn + dfn) <= ustarsq) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1845               dh = h\_at\_vel(i,k)}
\DoxyCodeLine{1846             \textcolor{keywordflow}{else}}
\DoxyCodeLine{1847               dh = h\_at\_vel(i,k) * sqrt((ustarsq-\/oldfn) / (dfn))}
\DoxyCodeLine{1848 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{1849 }
\DoxyCodeLine{1850             htot(i) = htot(i) + dh}
\DoxyCodeLine{1851             rhtot = rhtot + rlay*dh}
\DoxyCodeLine{1852 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{1853           \textcolor{keywordflow}{if} (gv\%Rlay(nz)*htot(i) -\/ rhtot(i) < ustarsq) \&}
\DoxyCodeLine{1854             htot(i) = htot(i) + h\_at\_vel(i,nz)}
\DoxyCodeLine{1855 \textcolor{keywordflow}{        endif} \textcolor{comment}{! use\_EOS}}
\DoxyCodeLine{1856 }
\DoxyCodeLine{1857        \textcolor{comment}{!visc\%tbl\_thick\_shelf\_v(i,J) = GV\%H\_to\_Z * max(CS\%Htbl\_shelf\_min, \&}}
\DoxyCodeLine{1858        \textcolor{comment}{!    htot(i) / (0.5 + sqrt(0.25 + \&}}
\DoxyCodeLine{1859        \textcolor{comment}{!        (htot(i)*(G\%CoriolisBu(I-\/1,J)+G\%CoriolisBu(I,J)))**2 / \&}}
\DoxyCodeLine{1860        \textcolor{comment}{!        (ustar(i)*GV\%Z\_to\_H)**2 )) )}}
\DoxyCodeLine{1861         ustar1 = ustar(i)*gv\%Z\_to\_H}
\DoxyCodeLine{1862         h2f2 = (htot(i)*(g\%CoriolisBu(i-\/1,j)+g\%CoriolisBu(i,j)) + h\_neglect*cs\%omega)**2}
\DoxyCodeLine{1863         tbl\_thick\_z = gv\%H\_to\_Z * max(cs\%Htbl\_shelf\_min, \&}
\DoxyCodeLine{1864             ( htot(i)*ustar1 ) / ( 0.5*ustar1 + sqrt((0.5*ustar1)**2 + h2f2 ) ) )}
\DoxyCodeLine{1865         visc\%tbl\_thick\_shelf\_v(i,j) = tbl\_thick\_z}
\DoxyCodeLine{1866         visc\%Kv\_tbl\_shelf\_v(i,j) = max(cs\%Kv\_TBL\_min, cdrag\_sqrt*ustar(i)*tbl\_thick\_z)}
\DoxyCodeLine{1867 }
\DoxyCodeLine{1868 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i-\/loop}}
\DoxyCodeLine{1869 \textcolor{keywordflow}{    endif} \textcolor{comment}{! do\_any\_shelf}}
\DoxyCodeLine{1870 }
\DoxyCodeLine{1871 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! J-\/loop at v-\/points}}
\DoxyCodeLine{1872 }
\DoxyCodeLine{1873   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1874     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%nkml\_visc\_u) .and. \textcolor{keyword}{associated}(visc\%nkml\_visc\_v)) \&}
\DoxyCodeLine{1875       \textcolor{keyword}{call }uvchksum(\textcolor{stringliteral}{"nkml\_visc\_[uv]"}, visc\%nkml\_visc\_u, visc\%nkml\_visc\_v, \&}
\DoxyCodeLine{1876                     g\%HI, haloshift=0, scalar\_pair=.true.)}
\DoxyCodeLine{1877 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1878   \textcolor{keywordflow}{if} (cs\%id\_nkml\_visc\_u > 0) \&}
\DoxyCodeLine{1879     \textcolor{keyword}{call }post\_data(cs\%id\_nkml\_visc\_u, visc\%nkml\_visc\_u, cs\%diag)}
\DoxyCodeLine{1880   \textcolor{keywordflow}{if} (cs\%id\_nkml\_visc\_v > 0) \&}
\DoxyCodeLine{1881     \textcolor{keyword}{call }post\_data(cs\%id\_nkml\_visc\_v, visc\%nkml\_visc\_v, cs\%diag)}
\DoxyCodeLine{1882 }

\end{DoxyCode}
