\hypertarget{namespacemom__diapyc__energy__req}{}\doxysection{mom\+\_\+diapyc\+\_\+energy\+\_\+req Module Reference}
\label{namespacemom__diapyc__energy__req}\index{mom\_diapyc\_energy\_req@{mom\_diapyc\_energy\_req}}


\doxysubsection{Detailed Description}
Calculates the energy requirements of mixing. \doxysubsection*{Data Types}
\begin{DoxyCompactItemize}
\item 
type \mbox{\hyperlink{structmom__diapyc__energy__req_1_1diapyc__energy__req__cs}{diapyc\+\_\+energy\+\_\+req\+\_\+cs}}
\begin{DoxyCompactList}\small\item\em This control structure holds parameters for the M\+O\+M\+\_\+diapyc\+\_\+energy\+\_\+req module. \end{DoxyCompactList}\end{DoxyCompactItemize}
\doxysubsection*{Functions/\+Subroutines}
\begin{DoxyCompactItemize}
\item 
subroutine, public \mbox{\hyperlink{namespacemom__diapyc__energy__req_a0bf0dd1f3ae4f7f66fb000322f18064e}{diapyc\+\_\+energy\+\_\+req\+\_\+test}} (h\+\_\+3d, dt, tv, G, GV, US, CS, Kd\+\_\+int)
\begin{DoxyCompactList}\small\item\em This subroutine helps test the accuracy of the diapycnal mixing energy requirement code by writing diagnostics, possibly using an intensely mixing test profile of diffusivity. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__diapyc__energy__req_aca27fbd2a716b8565b0b754a417479d5}{diapyc\+\_\+energy\+\_\+req\+\_\+calc}} (h\+\_\+in, T\+\_\+in, S\+\_\+in, Kd, energy\+\_\+\+Kd, dt, tv, G, GV, US, may\+\_\+print, CS)
\begin{DoxyCompactList}\small\item\em This subroutine uses a substantially refactored tridiagonal equation for diapycnal mixing of temperature and salinity to estimate the potential energy change due to diapycnal mixing in a column of water. It does this estimate 4 different ways, all of which should be equivalent, but reports only one. The various estimates are taken because they will later be used as templates for other bits of code. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__diapyc__energy__req_a2bed511a4b1df9de0e2230c24389bc82}{find\+\_\+pe\+\_\+chg}} (Kddt\+\_\+h0, d\+Kddt\+\_\+h, hp\+\_\+a, hp\+\_\+b, Th\+\_\+a, Sh\+\_\+a, Th\+\_\+b, Sh\+\_\+b, d\+T\+\_\+to\+\_\+d\+P\+E\+\_\+a, d\+S\+\_\+to\+\_\+d\+P\+E\+\_\+a, d\+T\+\_\+to\+\_\+d\+P\+E\+\_\+b, d\+S\+\_\+to\+\_\+d\+P\+E\+\_\+b, pres\+\_\+Z, d\+T\+\_\+to\+\_\+d\+Col\+Ht\+\_\+a, d\+S\+\_\+to\+\_\+d\+Col\+Ht\+\_\+a, d\+T\+\_\+to\+\_\+d\+Col\+Ht\+\_\+b, d\+S\+\_\+to\+\_\+d\+Col\+Ht\+\_\+b, P\+E\+\_\+chg, d\+P\+Ec\+\_\+d\+Kd, d\+P\+E\+\_\+max, d\+P\+Ec\+\_\+d\+Kd\+\_\+0, Col\+Ht\+\_\+cor)
\begin{DoxyCompactList}\small\item\em This subroutine calculates the change in potential energy and or derivatives for several changes in an interfaces\textquotesingle{}s diapycnal diffusivity times a timestep. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__diapyc__energy__req_afb1736a09e8f04ae84f561924e157691}{find\+\_\+pe\+\_\+chg\+\_\+orig}} (Kddt\+\_\+h, h\+\_\+k, b\+\_\+den\+\_\+1, d\+Te\+\_\+term, d\+Se\+\_\+term, d\+T\+\_\+km1\+\_\+t2, d\+S\+\_\+km1\+\_\+t2, d\+T\+\_\+to\+\_\+d\+P\+E\+\_\+k, d\+S\+\_\+to\+\_\+d\+P\+E\+\_\+k, d\+T\+\_\+to\+\_\+d\+P\+Ea, d\+S\+\_\+to\+\_\+d\+P\+Ea, pres\+\_\+Z, d\+T\+\_\+to\+\_\+d\+Col\+Ht\+\_\+k, d\+S\+\_\+to\+\_\+d\+Col\+Ht\+\_\+k, d\+T\+\_\+to\+\_\+d\+Col\+Hta, d\+S\+\_\+to\+\_\+d\+Col\+Hta, P\+E\+\_\+chg, d\+P\+Ec\+\_\+d\+Kd, d\+P\+E\+\_\+max, d\+P\+Ec\+\_\+d\+Kd\+\_\+0)
\begin{DoxyCompactList}\small\item\em This subroutine calculates the change in potential energy and or derivatives for several changes in an interfaces\textquotesingle{}s diapycnal diffusivity times a timestep using the original form used in the first version of e\+P\+BL. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__diapyc__energy__req_a63b127bfd78461d8df3449591792b224}{diapyc\+\_\+energy\+\_\+req\+\_\+init}} (Time, G, GV, US, param\+\_\+file, diag, CS)
\begin{DoxyCompactList}\small\item\em Initialize parameters and allocate memory associated with the diapycnal energy requirement module. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__diapyc__energy__req_aa4cb9f1b90a245e34ddd4182943a21c6}{diapyc\+\_\+energy\+\_\+req\+\_\+end}} (CS)
\begin{DoxyCompactList}\small\item\em Clean up and deallocate memory associated with the diapycnal energy requirement module. \end{DoxyCompactList}\end{DoxyCompactItemize}


\doxysubsection{Function/\+Subroutine Documentation}
\mbox{\Hypertarget{namespacemom__diapyc__energy__req_aca27fbd2a716b8565b0b754a417479d5}\label{namespacemom__diapyc__energy__req_aca27fbd2a716b8565b0b754a417479d5}} 
\index{mom\_diapyc\_energy\_req@{mom\_diapyc\_energy\_req}!diapyc\_energy\_req\_calc@{diapyc\_energy\_req\_calc}}
\index{diapyc\_energy\_req\_calc@{diapyc\_energy\_req\_calc}!mom\_diapyc\_energy\_req@{mom\_diapyc\_energy\_req}}
\doxysubsubsection{\texorpdfstring{diapyc\_energy\_req\_calc()}{diapyc\_energy\_req\_calc()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+diapyc\+\_\+energy\+\_\+req\+::diapyc\+\_\+energy\+\_\+req\+\_\+calc (\begin{DoxyParamCaption}\item[{real, dimension(gv\%ke), intent(in)}]{h\+\_\+in,  }\item[{real, dimension(gv\%ke), intent(in)}]{T\+\_\+in,  }\item[{real, dimension(gv\%ke), intent(in)}]{S\+\_\+in,  }\item[{real, dimension(gv\%ke+1), intent(in)}]{Kd,  }\item[{real, intent(out)}]{energy\+\_\+\+Kd,  }\item[{real, intent(in)}]{dt,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(inout)}]{tv,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{logical, intent(in), optional}]{may\+\_\+print,  }\item[{type(\mbox{\hyperlink{structmom__diapyc__energy__req_1_1diapyc__energy__req__cs}{diapyc\+\_\+energy\+\_\+req\+\_\+cs}}), optional, pointer}]{CS }\end{DoxyParamCaption})}



This subroutine uses a substantially refactored tridiagonal equation for diapycnal mixing of temperature and salinity to estimate the potential energy change due to diapycnal mixing in a column of water. It does this estimate 4 different ways, all of which should be equivalent, but reports only one. The various estimates are taken because they will later be used as templates for other bits of code. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & The ocean\textquotesingle{}s grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in}}  & {\em h\+\_\+in} & Layer thickness before entrainment, \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em t\+\_\+in} & The layer temperatures \mbox{[}degC\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em s\+\_\+in} & The layer salinities \mbox{[}ppt\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em kd} & The interfaces diapycnal diffusivities \mbox{[}Z2 T-\/1 $\sim$$>$ m2 s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & The amount of time covered by this call \mbox{[}T $\sim$$>$ s\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em energy\+\_\+kd} & The column-\/integrated rate of energy consumption by diapycnal diffusion \mbox{[}R Z L2 T-\/3 $\sim$$>$ W m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em tv} & A structure containing pointers to any available thermodynamic fields. Absent fields have N\+U\+LL ptrs. \\
\hline
\mbox{\texttt{ in}}  & {\em may\+\_\+print} & If present and true, write out diagnostics of energy use. \\
\hline
 & {\em cs} & This module\textquotesingle{}s control structure. \\
\hline
\end{DoxyParams}


Definition at line 122 of file M\+O\+M\+\_\+diapyc\+\_\+energy\+\_\+req.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{122   \textcolor{keywordtype}{type}(ocean\_grid\_type),    \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{    !< The ocean's grid structure.}}
\DoxyCodeLine{123   \textcolor{keywordtype}{type}(verticalGrid\_type),  \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure.}}
\DoxyCodeLine{124   \textcolor{keywordtype}{type}(unit\_scale\_type),    \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{   !< A dimensional unit scaling type}}
\DoxyCodeLine{125 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(GV\%ke)},   \textcolor{keywordtype}{intent(in)}    :: h\_in\textcolor{comment}{ !< Layer thickness before entrainment,}}
\DoxyCodeLine{126 \textcolor{comment}{                                                  !! [H ~> m or kg m-\/2].}}
\DoxyCodeLine{127 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(GV\%ke)},   \textcolor{keywordtype}{intent(in)}    :: T\_in\textcolor{comment}{ !< The layer temperatures [degC].}}
\DoxyCodeLine{128 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(GV\%ke)},   \textcolor{keywordtype}{intent(in)}    :: S\_in\textcolor{comment}{ !< The layer salinities [ppt].}}
\DoxyCodeLine{129 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(GV\%ke+1)}, \textcolor{keywordtype}{intent(in)}    :: Kd\textcolor{comment}{   !< The interfaces diapycnal diffusivities}}
\DoxyCodeLine{130 \textcolor{comment}{                                                  !! [Z2 T-\/1 ~> m2 s-\/1].}}
\DoxyCodeLine{131 \textcolor{keywordtype}{  real},                     \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{   !< The amount of time covered by this call [T ~> s].}}
\DoxyCodeLine{132 \textcolor{keywordtype}{  real},                     \textcolor{keywordtype}{intent(out)}   :: energy\_Kd\textcolor{comment}{ !< The column-\/integrated rate of energy}}
\DoxyCodeLine{133 \textcolor{comment}{                                                  !! consumption by diapycnal diffusion [R Z L2 T-\/3 ~> W m-\/2].}}
\DoxyCodeLine{134   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),    \textcolor{keywordtype}{intent(inout)} :: tv\textcolor{comment}{   !< A structure containing pointers to any}}
\DoxyCodeLine{135 \textcolor{comment}{                                                  !! available thermodynamic fields.}}
\DoxyCodeLine{136 \textcolor{comment}{                                                  !! Absent fields have NULL ptrs.}}
\DoxyCodeLine{137   \textcolor{keywordtype}{logical},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: may\_print\textcolor{comment}{ !< If present and true, write out diagnostics}}
\DoxyCodeLine{138 \textcolor{comment}{                                                  !! of energy use.}}
\DoxyCodeLine{139   \textcolor{keywordtype}{type}(diapyc\_energy\_req\_CS), \&}
\DoxyCodeLine{140                   \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{   !< This module's control structure.}}
\DoxyCodeLine{141 }
\DoxyCodeLine{142 \textcolor{comment}{!   This subroutine uses a substantially refactored tridiagonal equation for}}
\DoxyCodeLine{143 \textcolor{comment}{! diapycnal mixing of temperature and salinity to estimate the potential energy}}
\DoxyCodeLine{144 \textcolor{comment}{! change due to diapycnal mixing in a column of water.  It does this estimate}}
\DoxyCodeLine{145 \textcolor{comment}{! 4 different ways, all of which should be equivalent, but reports only one.}}
\DoxyCodeLine{146 \textcolor{comment}{! The various estimates are taken because they will later be used as templates}}
\DoxyCodeLine{147 \textcolor{comment}{! for other bits of code.}}
\DoxyCodeLine{148 }
\DoxyCodeLine{149 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(GV\%ke)} :: \&}
\DoxyCodeLine{150     p\_lay, \&    \textcolor{comment}{! Average pressure of a layer [R L2 T-\/2 ~> Pa].}}
\DoxyCodeLine{151     dSV\_dT, \&   \textcolor{comment}{! Partial derivative of specific volume with temperature [R-\/1 degC-\/1 ~> m3 kg-\/1 degC-\/1].}}
\DoxyCodeLine{152     dSV\_dS, \&   \textcolor{comment}{! Partial derivative of specific volume with salinity [R-\/1 ppt-\/1 ~> m3 kg-\/1 ppt-\/1].}}
\DoxyCodeLine{153     T0, S0, \&   \textcolor{comment}{! Initial temperatures and salinities [degC] and [ppt].}}
\DoxyCodeLine{154     Te, Se, \&   \textcolor{comment}{! Running incomplete estimates of the new temperatures and salinities [degC] and [ppt].}}
\DoxyCodeLine{155     Te\_a, Se\_a, \& \textcolor{comment}{! Running incomplete estimates of the new temperatures and salinities [degC] and [ppt].}}
\DoxyCodeLine{156     Te\_b, Se\_b, \& \textcolor{comment}{! Running incomplete estimates of the new temperatures and salinities [degC] and [ppt].}}
\DoxyCodeLine{157     Tf, Sf, \&   \textcolor{comment}{! New final values of the temperatures and salinities [degC] and [ppt].}}
\DoxyCodeLine{158     dTe, dSe, \& \textcolor{comment}{! Running (1-\/way) estimates of temperature and salinity change [degC] and [ppt].}}
\DoxyCodeLine{159     dTe\_a, dSe\_a, \& \textcolor{comment}{! Running (1-\/way) estimates of temperature and salinity change [degC] and [ppt].}}
\DoxyCodeLine{160     dTe\_b, dSe\_b, \& \textcolor{comment}{! Running (1-\/way) estimates of temperature and salinity change [degC] and [ppt].}}
\DoxyCodeLine{161     Th\_a, \&     \textcolor{comment}{! An effective temperature times a thickness in the layer above, including implicit}}
\DoxyCodeLine{162                 \textcolor{comment}{! mixing effects with other yet higher layers [degC H ~> degC m or degC kg m-\/2].}}
\DoxyCodeLine{163     sh\_a, \&     \textcolor{comment}{! An effective salinity times a thickness in the layer above, including implicit}}
\DoxyCodeLine{164                 \textcolor{comment}{! mixing effects with other yet higher layers [ppt H ~> ppt m or ppt kg m-\/2].}}
\DoxyCodeLine{165     th\_b, \&     \textcolor{comment}{! An effective temperature times a thickness in the layer below, including implicit}}
\DoxyCodeLine{166                 \textcolor{comment}{! mixing effects with other yet lower layers [degC H ~> degC m or degC kg m-\/2].}}
\DoxyCodeLine{167     sh\_b, \&     \textcolor{comment}{! An effective salinity times a thickness in the layer below, including implicit}}
\DoxyCodeLine{168                 \textcolor{comment}{! mixing effects with other yet lower layers [ppt H ~> ppt m or ppt kg m-\/2].}}
\DoxyCodeLine{169     dt\_to\_dpe, \& \textcolor{comment}{! Partial derivative of column potential energy with the temperature and salinity}}
\DoxyCodeLine{170     ds\_to\_dpe, \& \textcolor{comment}{! changes within a layer [R Z L2 T-\/2 degC-\/1 ~> J m-\/2 degC-\/1] and [R Z L2 T-\/2 ppt-\/1 ~> J m-\/2 ppt-\/1].}}
\DoxyCodeLine{171     dt\_to\_dcolht, \& \textcolor{comment}{! Partial derivatives of the total column height with the temperature}}
\DoxyCodeLine{172     ds\_to\_dcolht, \& \textcolor{comment}{! and salinity changes within a layer [Z degC-\/1 ~> m degC-\/1] and [Z ppt-\/1 ~> m ppt-\/1].}}
\DoxyCodeLine{173     dt\_to\_dcolht\_a, \& \textcolor{comment}{! Partial derivatives of the total column height with the temperature}}
\DoxyCodeLine{174     ds\_to\_dcolht\_a, \& \textcolor{comment}{! and salinity changes within a layer, including the implicit effects}}
\DoxyCodeLine{175                     \textcolor{comment}{! of mixing with layers higher in the water colun [Z degC-\/1 ~> m degC-\/1] and [Z ppt-\/1 ~> m ppt-\/1].}}
\DoxyCodeLine{176     dt\_to\_dcolht\_b, \& \textcolor{comment}{! Partial derivatives of the total column height with the temperature}}
\DoxyCodeLine{177     ds\_to\_dcolht\_b, \& \textcolor{comment}{! and salinity changes within a layer, including the implicit effects}}
\DoxyCodeLine{178                     \textcolor{comment}{! of mixing with layers lower in the water colun [Z degC-\/1 ~> m degC-\/1] and [Z ppt-\/1 ~> m ppt-\/1].}}
\DoxyCodeLine{179     dt\_to\_dpe\_a, \&  \textcolor{comment}{! Partial derivatives of column potential energy with the temperature}}
\DoxyCodeLine{180     ds\_to\_dpe\_a, \&  \textcolor{comment}{! and salinity changes within a layer, including the implicit effects}}
\DoxyCodeLine{181                     \textcolor{comment}{! of mixing with layers higher in the water column, in}}
\DoxyCodeLine{182                     \textcolor{comment}{! units of [R Z L2 T-\/2 degC-\/1 ~> J m-\/2 degC-\/1] and [R Z L2 T-\/2 ppt-\/1 ~> J m-\/2 ppt-\/1].}}
\DoxyCodeLine{183     dt\_to\_dpe\_b, \&  \textcolor{comment}{! Partial derivatives of column potential energy with the temperature}}
\DoxyCodeLine{184     ds\_to\_dpe\_b, \&  \textcolor{comment}{! and salinity changes within a layer, including the implicit effects}}
\DoxyCodeLine{185                     \textcolor{comment}{! of mixing with layers lower in the water column, in}}
\DoxyCodeLine{186                     \textcolor{comment}{! units of [R Z L2 T-\/2 degC-\/1 ~> J m-\/2 degC-\/1] and [R Z L2 T-\/2 ppt-\/1 ~> J m-\/2 ppt-\/1].}}
\DoxyCodeLine{187     hp\_a, \&     \textcolor{comment}{! An effective pivot thickness of the layer including the effects}}
\DoxyCodeLine{188                 \textcolor{comment}{! of coupling with layers above [H ~> m or kg m-\/2].  This is the first term}}
\DoxyCodeLine{189                 \textcolor{comment}{! in the denominator of b1 in a downward-\/oriented tridiagonal solver.}}
\DoxyCodeLine{190     hp\_b, \&     \textcolor{comment}{! An effective pivot thickness of the layer including the effects}}
\DoxyCodeLine{191                 \textcolor{comment}{! of coupling with layers below [H ~> m or kg m-\/2].  This is the first term}}
\DoxyCodeLine{192                 \textcolor{comment}{! in the denominator of b1 in an upward-\/oriented tridiagonal solver.}}
\DoxyCodeLine{193     c1\_a, \&     \textcolor{comment}{! c1\_a is used by a downward-\/oriented tridiagonal solver [nondim].}}
\DoxyCodeLine{194     c1\_b, \&     \textcolor{comment}{! c1\_b is used by an upward-\/oriented tridiagonal solver [nondim].}}
\DoxyCodeLine{195     h\_tr        \textcolor{comment}{! h\_tr is h at tracer points with a h\_neglect added to}}
\DoxyCodeLine{196                 \textcolor{comment}{! ensure positive definiteness [H ~> m or kg m-\/2].}}
\DoxyCodeLine{197 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(GV\%ke+1)} :: \&}
\DoxyCodeLine{198     pres, \&     \textcolor{comment}{! Interface pressures [R L2 T-\/2 ~> Pa].}}
\DoxyCodeLine{199     pres\_Z, \&   \textcolor{comment}{! Interface pressures with a rescaling factor to convert interface height}}
\DoxyCodeLine{200                 \textcolor{comment}{! movements into changes in column potential energy [R L2 T-\/2 m Z-\/1 ~> J m-\/3].}}
\DoxyCodeLine{201     z\_int, \&    \textcolor{comment}{! Interface heights relative to the surface [H ~> m or kg m-\/2].}}
\DoxyCodeLine{202     n2, \&       \textcolor{comment}{! An estimate of the buoyancy frequency [T-\/2 ~> s-\/2].}}
\DoxyCodeLine{203     kddt\_h, \&   \textcolor{comment}{! The diapycnal diffusivity times a timestep divided by the}}
\DoxyCodeLine{204                 \textcolor{comment}{! average thicknesses around a layer [H ~> m or kg m-\/2].}}
\DoxyCodeLine{205     kddt\_h\_a, \& \textcolor{comment}{! The value of Kddt\_h for layers above the central point in the}}
\DoxyCodeLine{206                 \textcolor{comment}{! tridiagonal solver [H ~> m or kg m-\/2].}}
\DoxyCodeLine{207     kddt\_h\_b, \& \textcolor{comment}{! The value of Kddt\_h for layers below the central point in the}}
\DoxyCodeLine{208                 \textcolor{comment}{! tridiagonal solver [H ~> m or kg m-\/2].}}
\DoxyCodeLine{209     kd\_so\_far   \textcolor{comment}{! The value of Kddt\_h that has been applied already in}}
\DoxyCodeLine{210                 \textcolor{comment}{! calculating the energy changes [H ~> m or kg m-\/2].}}
\DoxyCodeLine{211 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(GV\%ke+1,4)} :: \&}
\DoxyCodeLine{212     PE\_chg\_k, \&     \textcolor{comment}{! The integrated potential energy change within a timestep due}}
\DoxyCodeLine{213                     \textcolor{comment}{! to the diffusivity at interface K for 4 different orders of}}
\DoxyCodeLine{214                     \textcolor{comment}{! accumulating the diffusivities [R Z L2 T-\/2 ~> J m-\/2].}}
\DoxyCodeLine{215     colht\_cor\_k     \textcolor{comment}{! The correction to the potential energy change due to}}
\DoxyCodeLine{216                     \textcolor{comment}{! changes in the net column height [R Z L2 T-\/2 ~> J m-\/2].}}
\DoxyCodeLine{217 \textcolor{keywordtype}{  real} :: \&}
\DoxyCodeLine{218     b1              \textcolor{comment}{! b1 is used by the tridiagonal solver [H-\/1 ~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{219 \textcolor{keywordtype}{  real} :: \&}
\DoxyCodeLine{220     I\_b1            \textcolor{comment}{! The inverse of b1 [H ~> m or kg m-\/2].}}
\DoxyCodeLine{221 \textcolor{keywordtype}{  real} :: Kd0       \textcolor{comment}{! The value of Kddt\_h that has already been applied [H ~> m or kg m-\/2].}}
\DoxyCodeLine{222 \textcolor{keywordtype}{  real} :: dKd       \textcolor{comment}{! The change in the value of Kddt\_h [H ~> m or kg m-\/2].}}
\DoxyCodeLine{223 \textcolor{keywordtype}{  real} :: h\_neglect \textcolor{comment}{! A thickness that is so small it is usually lost}}
\DoxyCodeLine{224                     \textcolor{comment}{! in roundoff and can be neglected [H ~> m or kg m-\/2].}}
\DoxyCodeLine{225 \textcolor{keywordtype}{  real} :: dTe\_term  \textcolor{comment}{! A diffusivity-\/independent term related to the temperature}}
\DoxyCodeLine{226                     \textcolor{comment}{! change in the layer below the interface [degC H ~> degC m or degC kg m-\/2].}}
\DoxyCodeLine{227 \textcolor{keywordtype}{  real} :: dSe\_term  \textcolor{comment}{! A diffusivity-\/independent term related to the salinity}}
\DoxyCodeLine{228                     \textcolor{comment}{! change in the layer below the interface [ppt H ~> ppt m or ppt kg m-\/2].}}
\DoxyCodeLine{229 \textcolor{keywordtype}{  real} :: Kddt\_h\_guess \textcolor{comment}{! A guess of the final value of Kddt\_h [H ~> m or kg m-\/2].}}
\DoxyCodeLine{230 \textcolor{keywordtype}{  real} :: dMass     \textcolor{comment}{! The mass per unit area within a layer [R Z ~> kg m-\/2].}}
\DoxyCodeLine{231 \textcolor{keywordtype}{  real} :: dPres     \textcolor{comment}{! The hydrostatic pressure change across a layer [R L2 T-\/2 ~> Pa].}}
\DoxyCodeLine{232 \textcolor{keywordtype}{  real} :: dMKE\_max  \textcolor{comment}{! The maximum amount of mean kinetic energy that could be}}
\DoxyCodeLine{233                     \textcolor{comment}{! converted to turbulent kinetic energy if the velocity in}}
\DoxyCodeLine{234                     \textcolor{comment}{! the layer below an interface were homogenized with all of}}
\DoxyCodeLine{235                     \textcolor{comment}{! the water above the interface [R Z L2 T-\/2 ~> J m-\/2 = kg s-\/2].}}
\DoxyCodeLine{236 \textcolor{keywordtype}{  real} :: rho\_here  \textcolor{comment}{! The in-\/situ density [R ~> kg m-\/3].}}
\DoxyCodeLine{237 \textcolor{keywordtype}{  real} :: PE\_change \textcolor{comment}{! The change in column potential energy from applying Kddt\_h at the}}
\DoxyCodeLine{238                     \textcolor{comment}{! present interface [R L2 Z T-\/2 ~> J m-\/2].}}
\DoxyCodeLine{239 \textcolor{keywordtype}{  real} :: ColHt\_cor \textcolor{comment}{! The correction to PE\_chg that is made due to a net}}
\DoxyCodeLine{240                     \textcolor{comment}{! change in the column height [R L2 Z T-\/2 ~> J m-\/2].}}
\DoxyCodeLine{241 \textcolor{keywordtype}{  real} :: htot      \textcolor{comment}{! A running sum of thicknesses [H ~> m or kg m-\/2].}}
\DoxyCodeLine{242 \textcolor{keywordtype}{  real} :: dTe\_t2, dT\_km1\_t2, dT\_k\_t2  \textcolor{comment}{! Temporary arrays describing temperature changes [degC].}}
\DoxyCodeLine{243 \textcolor{keywordtype}{  real} :: dSe\_t2, dS\_km1\_t2, dS\_k\_t2  \textcolor{comment}{! Temporary arrays describing salinity changes [ppt].}}
\DoxyCodeLine{244   \textcolor{keywordtype}{logical} :: do\_print}
\DoxyCodeLine{245 }
\DoxyCodeLine{246   \textcolor{comment}{! The following are a bunch of diagnostic arrays for debugging purposes.}}
\DoxyCodeLine{247 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(GV\%ke)} :: \&}
\DoxyCodeLine{248     Ta, Sa, Tb, Sb}
\DoxyCodeLine{249 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(GV\%ke+1)} :: \&}
\DoxyCodeLine{250     dPEa\_dKd, dPEa\_dKd\_est, dPEa\_dKd\_err, dPEa\_dKd\_trunc, dPEa\_dKd\_err\_norm, \&}
\DoxyCodeLine{251     dPEb\_dKd, dPEb\_dKd\_est, dPEb\_dKd\_err, dPEb\_dKd\_trunc, dPEb\_dKd\_err\_norm}
\DoxyCodeLine{252 \textcolor{keywordtype}{  real} :: PE\_chg\_tot1A, PE\_chg\_tot2A, T\_chg\_totA}
\DoxyCodeLine{253 \textcolor{keywordtype}{  real} :: PE\_chg\_tot1B, PE\_chg\_tot2B, T\_chg\_totB}
\DoxyCodeLine{254 \textcolor{keywordtype}{  real} :: PE\_chg\_tot1C, PE\_chg\_tot2C, T\_chg\_totC}
\DoxyCodeLine{255 \textcolor{keywordtype}{  real} :: PE\_chg\_tot1D, PE\_chg\_tot2D, T\_chg\_totD}
\DoxyCodeLine{256 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(GV\%ke+1)}  :: dPEchg\_dKd}
\DoxyCodeLine{257 \textcolor{keywordtype}{  real} :: PE\_chg(6)}
\DoxyCodeLine{258 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(6)} :: dT\_k\_itt, dS\_k\_itt, dT\_km1\_itt, dS\_km1\_itt}
\DoxyCodeLine{259 }
\DoxyCodeLine{260   \textcolor{keywordtype}{integer} :: k, nz, itt, max\_itt, k\_cent}
\DoxyCodeLine{261   \textcolor{keywordtype}{logical} :: surface\_BL, bottom\_BL, central, halves, debug}
\DoxyCodeLine{262   \textcolor{keywordtype}{logical} :: old\_PE\_calc}
\DoxyCodeLine{263   nz = g\%ke}
\DoxyCodeLine{264   h\_neglect = gv\%H\_subroundoff}
\DoxyCodeLine{265 }
\DoxyCodeLine{266   debug = .true.}
\DoxyCodeLine{267 }
\DoxyCodeLine{268   surface\_bl = .true. ; bottom\_bl = .true. ; halves = .true.}
\DoxyCodeLine{269   central = .true. ; k\_cent = nz/2}
\DoxyCodeLine{270 }
\DoxyCodeLine{271   do\_print = .false. ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(may\_print) .and. \textcolor{keyword}{present}(cs)) do\_print = may\_print}
\DoxyCodeLine{272 }
\DoxyCodeLine{273   dpea\_dkd(:) = 0.0 ; dpea\_dkd\_est(:) = 0.0 ; dpea\_dkd\_err(:) = 0.0}
\DoxyCodeLine{274   dpea\_dkd\_err\_norm(:) = 0.0 ; dpea\_dkd\_trunc(:) = 0.0}
\DoxyCodeLine{275   dpeb\_dkd(:) = 0.0 ; dpeb\_dkd\_est(:) = 0.0 ; dpeb\_dkd\_err(:) = 0.0}
\DoxyCodeLine{276   dpeb\_dkd\_err\_norm(:) = 0.0 ; dpeb\_dkd\_trunc(:) = 0.0}
\DoxyCodeLine{277 }
\DoxyCodeLine{278   htot = 0.0 ; pres(1) = 0.0 ; pres\_z(1) = 0.0 ; z\_int(1) = 0.0}
\DoxyCodeLine{279   \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{280     t0(k) = t\_in(k) ; s0(k) = s\_in(k)}
\DoxyCodeLine{281     h\_tr(k) = h\_in(k)}
\DoxyCodeLine{282     htot = htot + h\_tr(k)}
\DoxyCodeLine{283     pres(k+1) = pres(k) + (gv\%g\_Earth * gv\%H\_to\_RZ) * h\_tr(k)}
\DoxyCodeLine{284     pres\_z(k+1) = pres(k+1)}
\DoxyCodeLine{285     p\_lay(k) = 0.5*(pres(k) + pres(k+1))}
\DoxyCodeLine{286     z\_int(k+1) = z\_int(k) -\/ h\_tr(k)}
\DoxyCodeLine{287 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{288   \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{289     h\_tr(k) = max(h\_tr(k), 1e-\/15*htot)}
\DoxyCodeLine{290 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{291 }
\DoxyCodeLine{292   \textcolor{comment}{! Introduce a diffusive flux variable, Kddt\_h(K) = ea(k) = eb(k-\/1)}}
\DoxyCodeLine{293 }
\DoxyCodeLine{294   kddt\_h(1) = 0.0 ; kddt\_h(nz+1) = 0.0}
\DoxyCodeLine{295   \textcolor{keywordflow}{do} k=2,nz}
\DoxyCodeLine{296     kddt\_h(k) = min((gv\%Z\_to\_H**2*dt)*kd(k) / (0.5*(h\_tr(k-\/1) + h\_tr(k))), 1e3*htot)}
\DoxyCodeLine{297 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{298 }
\DoxyCodeLine{299   \textcolor{comment}{! Solve the tridiagonal equations for new temperatures.}}
\DoxyCodeLine{300 }
\DoxyCodeLine{301   \textcolor{keyword}{call }calculate\_specific\_vol\_derivs(t0, s0, p\_lay, dsv\_dt, dsv\_ds, tv\%eqn\_of\_state)}
\DoxyCodeLine{302 }
\DoxyCodeLine{303   \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{304     dmass = gv\%H\_to\_RZ * h\_tr(k)}
\DoxyCodeLine{305     dpres = (gv\%g\_Earth * gv\%H\_to\_RZ) * h\_tr(k)}
\DoxyCodeLine{306     dt\_to\_dpe(k) = (dmass * (pres(k) + 0.5*dpres)) * dsv\_dt(k)}
\DoxyCodeLine{307     ds\_to\_dpe(k) = (dmass * (pres(k) + 0.5*dpres)) * dsv\_ds(k)}
\DoxyCodeLine{308     dt\_to\_dcolht(k) = dmass * dsv\_dt(k) * cs\%ColHt\_scaling}
\DoxyCodeLine{309     ds\_to\_dcolht(k) = dmass * dsv\_ds(k) * cs\%ColHt\_scaling}
\DoxyCodeLine{310 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{311 }
\DoxyCodeLine{312 \textcolor{comment}{!  PE\_chg\_k(1) = 0.0 ; PE\_chg\_k(nz+1) = 0.0}}
\DoxyCodeLine{313   \textcolor{comment}{! PEchg(:) = 0.0}}
\DoxyCodeLine{314   pe\_chg\_k(:,:) = 0.0 ; colht\_cor\_k(:,:) = 0.0}
\DoxyCodeLine{315   dpechg\_dkd(:) = 0.0}
\DoxyCodeLine{316 }
\DoxyCodeLine{317   \textcolor{keywordflow}{if} (surface\_bl) \textcolor{keywordflow}{then}  \textcolor{comment}{! This version is appropriate for a surface boundary layer.}}
\DoxyCodeLine{318     old\_pe\_calc = .false.}
\DoxyCodeLine{319 }
\DoxyCodeLine{320     \textcolor{comment}{! Set up values appropriate for no diffusivity.}}
\DoxyCodeLine{321     \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{322       hp\_a(k) = h\_tr(k) ; hp\_b(k) = h\_tr(k)}
\DoxyCodeLine{323       dt\_to\_dpe\_a(k) = dt\_to\_dpe(k) ; ds\_to\_dpe\_a(k) = ds\_to\_dpe(k)}
\DoxyCodeLine{324       dt\_to\_dpe\_b(k) = dt\_to\_dpe(k) ; ds\_to\_dpe\_b(k) = ds\_to\_dpe(k)}
\DoxyCodeLine{325       dt\_to\_dcolht\_a(k) = dt\_to\_dcolht(k) ; ds\_to\_dcolht\_a(k) = ds\_to\_dcolht(k)}
\DoxyCodeLine{326       dt\_to\_dcolht\_b(k) = dt\_to\_dcolht(k) ; ds\_to\_dcolht\_b(k) = ds\_to\_dcolht(k)}
\DoxyCodeLine{327 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{328 }
\DoxyCodeLine{329     \textcolor{keywordflow}{do} k=2,nz}
\DoxyCodeLine{330       \textcolor{comment}{! At this point, Kddt\_h(K) will be unknown because its value may depend}}
\DoxyCodeLine{331       \textcolor{comment}{! on how much energy is available.}}
\DoxyCodeLine{332 }
\DoxyCodeLine{333       \textcolor{comment}{! Precalculate some temporary expressions that are independent of Kddt\_h\_guess.}}
\DoxyCodeLine{334       \textcolor{keywordflow}{if} (old\_pe\_calc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{335         \textcolor{keywordflow}{if} (k==2) \textcolor{keywordflow}{then}}
\DoxyCodeLine{336           dt\_km1\_t2 = (t0(k)-\/t0(k-\/1))}
\DoxyCodeLine{337           ds\_km1\_t2 = (s0(k)-\/s0(k-\/1))}
\DoxyCodeLine{338           dte\_t2 = 0.0 ; dse\_t2 = 0.0}
\DoxyCodeLine{339         \textcolor{keywordflow}{else}}
\DoxyCodeLine{340           dte\_t2 = kddt\_h(k-\/1) * ((t0(k-\/2) -\/ t0(k-\/1)) + dte(k-\/2))}
\DoxyCodeLine{341           dse\_t2 = kddt\_h(k-\/1) * ((s0(k-\/2) -\/ s0(k-\/1)) + dse(k-\/2))}
\DoxyCodeLine{342           dt\_km1\_t2 = (t0(k)-\/t0(k-\/1)) -\/ \&}
\DoxyCodeLine{343                 (kddt\_h(k-\/1) / hp\_a(k-\/1)) * ((t0(k-\/2) -\/ t0(k-\/1)) + dte(k-\/2))}
\DoxyCodeLine{344           ds\_km1\_t2 = (s0(k)-\/s0(k-\/1)) -\/ \&}
\DoxyCodeLine{345                 (kddt\_h(k-\/1) / hp\_a(k-\/1)) * ((s0(k-\/2) -\/ s0(k-\/1)) + dse(k-\/2))}
\DoxyCodeLine{346 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{347         dte\_term = dte\_t2 + hp\_a(k-\/1) * (t0(k-\/1)-\/t0(k))}
\DoxyCodeLine{348         dse\_term = dse\_t2 + hp\_a(k-\/1) * (s0(k-\/1)-\/s0(k))}
\DoxyCodeLine{349       \textcolor{keywordflow}{else}}
\DoxyCodeLine{350         \textcolor{keywordflow}{if} (k<=2) \textcolor{keywordflow}{then}}
\DoxyCodeLine{351           th\_a(k-\/1) = h\_tr(k-\/1) * t0(k-\/1) ; sh\_a(k-\/1) = h\_tr(k-\/1) * s0(k-\/1)}
\DoxyCodeLine{352         \textcolor{keywordflow}{else}}
\DoxyCodeLine{353           th\_a(k-\/1) = h\_tr(k-\/1) * t0(k-\/1) + kddt\_h(k-\/1) * te(k-\/2)}
\DoxyCodeLine{354           sh\_a(k-\/1) = h\_tr(k-\/1) * s0(k-\/1) + kddt\_h(k-\/1) * se(k-\/2)}
\DoxyCodeLine{355 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{356         th\_b(k) = h\_tr(k) * t0(k) ; sh\_b(k) = h\_tr(k) * s0(k)}
\DoxyCodeLine{357 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{358 }
\DoxyCodeLine{359 }
\DoxyCodeLine{360       \textcolor{comment}{! Find the energy change due to a guess at the strength of diffusion at interface K.}}
\DoxyCodeLine{361 }
\DoxyCodeLine{362       kddt\_h\_guess = kddt\_h(k)}
\DoxyCodeLine{363       \textcolor{keywordflow}{if} (old\_pe\_calc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{364         \textcolor{keyword}{call }find\_pe\_chg\_orig(kddt\_h\_guess, h\_tr(k), hp\_a(k-\/1), \&}
\DoxyCodeLine{365                          dte\_term, dse\_term, dt\_km1\_t2, ds\_km1\_t2, \&}
\DoxyCodeLine{366                          dt\_to\_dpe(k), ds\_to\_dpe(k), dt\_to\_dpe\_a(k-\/1), ds\_to\_dpe\_a(k-\/1), \&}
\DoxyCodeLine{367                          pres\_z(k), dt\_to\_dcolht(k), ds\_to\_dcolht(k), \&}
\DoxyCodeLine{368                          dt\_to\_dcolht\_a(k-\/1), ds\_to\_dcolht\_a(k-\/1), \&}
\DoxyCodeLine{369                          pe\_chg\_k(k,1), dpea\_dkd(k))}
\DoxyCodeLine{370       \textcolor{keywordflow}{else}}
\DoxyCodeLine{371         \textcolor{keyword}{call }find\_pe\_chg(0.0, kddt\_h\_guess, hp\_a(k-\/1), hp\_b(k), \&}
\DoxyCodeLine{372                          th\_a(k-\/1), sh\_a(k-\/1), th\_b(k), sh\_b(k), \&}
\DoxyCodeLine{373                          dt\_to\_dpe\_a(k-\/1), ds\_to\_dpe\_a(k-\/1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), \&}
\DoxyCodeLine{374                          pres\_z(k), dt\_to\_dcolht\_a(k-\/1), ds\_to\_dcolht\_a(k-\/1), \&}
\DoxyCodeLine{375                          dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), \&}
\DoxyCodeLine{376                          pe\_chg=pe\_chg\_k(k,1), dpec\_dkd=dpea\_dkd(k), \&}
\DoxyCodeLine{377                          colht\_cor=colht\_cor\_k(k,1))}
\DoxyCodeLine{378 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{379 }
\DoxyCodeLine{380       \textcolor{keywordflow}{if} (debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{381         \textcolor{keywordflow}{do} itt=1,5}
\DoxyCodeLine{382           kddt\_h\_guess = (1.0+0.01*(itt-\/3))*kddt\_h(k)}
\DoxyCodeLine{383 }
\DoxyCodeLine{384           \textcolor{keywordflow}{if} (old\_pe\_calc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{385             \textcolor{keyword}{call }find\_pe\_chg\_orig(kddt\_h\_guess, h\_tr(k), hp\_a(k-\/1), \&}
\DoxyCodeLine{386                              dte\_term, dse\_term, dt\_km1\_t2, ds\_km1\_t2, \&}
\DoxyCodeLine{387                              dt\_to\_dpe(k), ds\_to\_dpe(k), dt\_to\_dpe\_a(k-\/1), ds\_to\_dpe\_a(k-\/1), \&}
\DoxyCodeLine{388                              pres\_z(k), dt\_to\_dcolht(k), ds\_to\_dcolht(k), \&}
\DoxyCodeLine{389                              dt\_to\_dcolht\_a(k-\/1), ds\_to\_dcolht\_a(k-\/1), \&}
\DoxyCodeLine{390                              pe\_chg=pe\_chg(itt))}
\DoxyCodeLine{391           \textcolor{keywordflow}{else}}
\DoxyCodeLine{392             \textcolor{keyword}{call }find\_pe\_chg(0.0, kddt\_h\_guess, hp\_a(k-\/1), hp\_b(k), \&}
\DoxyCodeLine{393                              th\_a(k-\/1), sh\_a(k-\/1), th\_b(k), sh\_b(k), \&}
\DoxyCodeLine{394                              dt\_to\_dpe\_a(k-\/1), ds\_to\_dpe\_a(k-\/1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), \&}
\DoxyCodeLine{395                              pres\_z(k), dt\_to\_dcolht\_a(k-\/1), ds\_to\_dcolht\_a(k-\/1), \&}
\DoxyCodeLine{396                              dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), \&}
\DoxyCodeLine{397                              pe\_chg=pe\_chg(itt))}
\DoxyCodeLine{398 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{399 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{400         \textcolor{comment}{! Compare with a 4th-\/order finite difference estimate.}}
\DoxyCodeLine{401         dpea\_dkd\_est(k) = (4.0*(pe\_chg(4)-\/pe\_chg(2))/(0.02*kddt\_h(k)) -\/ \&}
\DoxyCodeLine{402                                (pe\_chg(5)-\/pe\_chg(1))/(0.04*kddt\_h(k))) / 3.0}
\DoxyCodeLine{403         dpea\_dkd\_trunc(k) = (pe\_chg(4)-\/pe\_chg(2))/(0.02*kddt\_h(k)) -\/ \&}
\DoxyCodeLine{404                             (pe\_chg(5)-\/pe\_chg(1))/(0.04*kddt\_h(k))}
\DoxyCodeLine{405         dpea\_dkd\_err(k) = (dpea\_dkd\_est(k) -\/ dpea\_dkd(k))}
\DoxyCodeLine{406         dpea\_dkd\_err\_norm(k) = (dpea\_dkd\_est(k) -\/ dpea\_dkd(k)) / \&}
\DoxyCodeLine{407                               (abs(dpea\_dkd\_est(k)) + abs(dpea\_dkd(k)) + 1e-\/100*us\%RZ\_to\_kg\_m2*us\%L\_T\_to\_m\_s**2)}
\DoxyCodeLine{408 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{409 }
\DoxyCodeLine{410       \textcolor{comment}{!   At this point, the final value of Kddt\_h(K) is known, so the estimated}}
\DoxyCodeLine{411       \textcolor{comment}{! properties for layer k-\/1 can be calculated.}}
\DoxyCodeLine{412 }
\DoxyCodeLine{413       b1 = 1.0 / (hp\_a(k-\/1) + kddt\_h(k))}
\DoxyCodeLine{414       c1\_a(k) = kddt\_h(k) * b1}
\DoxyCodeLine{415       \textcolor{keywordflow}{if} (k==2) \textcolor{keywordflow}{then}}
\DoxyCodeLine{416         te(1) = b1*(h\_tr(1)*t0(1))}
\DoxyCodeLine{417         se(1) = b1*(h\_tr(1)*s0(1))}
\DoxyCodeLine{418       \textcolor{keywordflow}{else}}
\DoxyCodeLine{419         te(k-\/1) = b1 * (h\_tr(k-\/1) * t0(k-\/1) + kddt\_h(k-\/1) * te(k-\/2))}
\DoxyCodeLine{420         se(k-\/1) = b1 * (h\_tr(k-\/1) * s0(k-\/1) + kddt\_h(k-\/1) * se(k-\/2))}
\DoxyCodeLine{421 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{422       \textcolor{keywordflow}{if} (old\_pe\_calc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{423         dte(k-\/1) = b1 * ( kddt\_h(k)*(t0(k)-\/t0(k-\/1)) + dte\_t2 )}
\DoxyCodeLine{424         dse(k-\/1) = b1 * ( kddt\_h(k)*(s0(k)-\/s0(k-\/1)) + dse\_t2 )}
\DoxyCodeLine{425 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{426 }
\DoxyCodeLine{427       hp\_a(k) = h\_tr(k) + (hp\_a(k-\/1) * b1) * kddt\_h(k)}
\DoxyCodeLine{428       dt\_to\_dpe\_a(k) = dt\_to\_dpe(k) + c1\_a(k)*dt\_to\_dpe\_a(k-\/1)}
\DoxyCodeLine{429       ds\_to\_dpe\_a(k) = ds\_to\_dpe(k) + c1\_a(k)*ds\_to\_dpe\_a(k-\/1)}
\DoxyCodeLine{430       dt\_to\_dcolht\_a(k) = dt\_to\_dcolht(k) + c1\_a(k)*dt\_to\_dcolht\_a(k-\/1)}
\DoxyCodeLine{431       ds\_to\_dcolht\_a(k) = ds\_to\_dcolht(k) + c1\_a(k)*ds\_to\_dcolht\_a(k-\/1)}
\DoxyCodeLine{432 }
\DoxyCodeLine{433 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{434 }
\DoxyCodeLine{435     b1 = 1.0 / (hp\_a(nz))}
\DoxyCodeLine{436     tf(nz) = b1 * (h\_tr(nz) * t0(nz) + kddt\_h(nz) * te(nz-\/1))}
\DoxyCodeLine{437     sf(nz) = b1 * (h\_tr(nz) * s0(nz) + kddt\_h(nz) * se(nz-\/1))}
\DoxyCodeLine{438     \textcolor{keywordflow}{if} (old\_pe\_calc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{439       dte(nz) = b1 * kddt\_h(nz) * ((t0(nz-\/1)-\/t0(nz)) + dte(nz-\/1))}
\DoxyCodeLine{440       dse(nz) = b1 * kddt\_h(nz) * ((s0(nz-\/1)-\/s0(nz)) + dse(nz-\/1))}
\DoxyCodeLine{441 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{442 }
\DoxyCodeLine{443     \textcolor{keywordflow}{do} k=nz-\/1,1,-\/1}
\DoxyCodeLine{444       tf(k) = te(k) + c1\_a(k+1)*tf(k+1)}
\DoxyCodeLine{445       sf(k) = se(k) + c1\_a(k+1)*sf(k+1)}
\DoxyCodeLine{446 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{447 }
\DoxyCodeLine{448     \textcolor{keywordflow}{if} (debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{449       \textcolor{keywordflow}{do} k=1,nz ; ta(k) = tf(k) ; sa(k) = sf(k) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{450       pe\_chg\_tot1a = 0.0 ; pe\_chg\_tot2a = 0.0 ; t\_chg\_tota = 0.0}
\DoxyCodeLine{451       \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{452         pe\_chg\_tot1a = pe\_chg\_tot1a + (dt\_to\_dpe(k) * (tf(k) -\/ t0(k)) + \&}
\DoxyCodeLine{453                                        ds\_to\_dpe(k) * (sf(k) -\/ s0(k)))}
\DoxyCodeLine{454         t\_chg\_tota = t\_chg\_tota + h\_tr(k) * (tf(k) -\/ t0(k))}
\DoxyCodeLine{455 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{456       \textcolor{keywordflow}{do} k=2,nz}
\DoxyCodeLine{457         pe\_chg\_tot2a = pe\_chg\_tot2a + (pe\_chg\_k(k,1) -\/ colht\_cor\_k(k,1))}
\DoxyCodeLine{458 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{459 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{460 }
\DoxyCodeLine{461 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{462 }
\DoxyCodeLine{463   \textcolor{keywordflow}{if} (bottom\_bl) \textcolor{keywordflow}{then}  \textcolor{comment}{! This version is appropriate for a bottom boundary layer.}}
\DoxyCodeLine{464     old\_pe\_calc = .false.}
\DoxyCodeLine{465 }
\DoxyCodeLine{466     \textcolor{comment}{! Set up values appropriate for no diffusivity.}}
\DoxyCodeLine{467     \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{468       hp\_a(k) = h\_tr(k) ; hp\_b(k) = h\_tr(k)}
\DoxyCodeLine{469       dt\_to\_dpe\_a(k) = dt\_to\_dpe(k) ; ds\_to\_dpe\_a(k) = ds\_to\_dpe(k)}
\DoxyCodeLine{470       dt\_to\_dpe\_b(k) = dt\_to\_dpe(k) ; ds\_to\_dpe\_b(k) = ds\_to\_dpe(k)}
\DoxyCodeLine{471       dt\_to\_dcolht\_a(k) = dt\_to\_dcolht(k) ; ds\_to\_dcolht\_a(k) = ds\_to\_dcolht(k)}
\DoxyCodeLine{472       dt\_to\_dcolht\_b(k) = dt\_to\_dcolht(k) ; ds\_to\_dcolht\_b(k) = ds\_to\_dcolht(k)}
\DoxyCodeLine{473 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{474 }
\DoxyCodeLine{475     \textcolor{keywordflow}{do} k=nz,2,-\/1  \textcolor{comment}{! Loop over interior interfaces.}}
\DoxyCodeLine{476       \textcolor{comment}{! At this point, Kddt\_h(K) will be unknown because its value may depend}}
\DoxyCodeLine{477       \textcolor{comment}{! on how much energy is available.}}
\DoxyCodeLine{478 }
\DoxyCodeLine{479       \textcolor{comment}{! Precalculate some temporary expressions that are independent of Kddt\_h\_guess.}}
\DoxyCodeLine{480       \textcolor{keywordflow}{if} (old\_pe\_calc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{481         \textcolor{keywordflow}{if} (k==nz) \textcolor{keywordflow}{then}}
\DoxyCodeLine{482           dt\_k\_t2 = (t0(k-\/1)-\/t0(k))}
\DoxyCodeLine{483           ds\_k\_t2 = (s0(k-\/1)-\/s0(k))}
\DoxyCodeLine{484           dte\_t2 = 0.0 ; dse\_t2 = 0.0}
\DoxyCodeLine{485         \textcolor{keywordflow}{else}}
\DoxyCodeLine{486           dte\_t2 = kddt\_h(k+1) * ((t0(k+1) -\/ t0(k)) + dte(k+1))}
\DoxyCodeLine{487           dse\_t2 = kddt\_h(k+1) * ((s0(k+1) -\/ s0(k)) + dse(k+1))}
\DoxyCodeLine{488           dt\_k\_t2 = (t0(k-\/1)-\/t0(k)) -\/ \&}
\DoxyCodeLine{489                   (kddt\_h(k+1)/ hp\_b(k)) * ((t0(k+1) -\/ t0(k)) + dte(k+1))}
\DoxyCodeLine{490           ds\_k\_t2 = (s0(k-\/1)-\/s0(k)) -\/ \&}
\DoxyCodeLine{491                   (kddt\_h(k+1)/ hp\_b(k)) * ((s0(k+1) -\/ s0(k)) + dse(k+1))}
\DoxyCodeLine{492 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{493         dte\_term = dte\_t2 + hp\_b(k) * (t0(k)-\/t0(k-\/1))}
\DoxyCodeLine{494         dse\_term = dse\_t2 + hp\_b(k) * (s0(k)-\/s0(k-\/1))}
\DoxyCodeLine{495       \textcolor{keywordflow}{else}}
\DoxyCodeLine{496         th\_a(k-\/1) = h\_tr(k-\/1) * t0(k-\/1) ; sh\_a(k-\/1) = h\_tr(k-\/1) * s0(k-\/1)}
\DoxyCodeLine{497         \textcolor{keywordflow}{if} (k>=nz) \textcolor{keywordflow}{then}}
\DoxyCodeLine{498           th\_b(k) = h\_tr(k) * t0(k) ; sh\_b(k) = h\_tr(k) * s0(k)}
\DoxyCodeLine{499         \textcolor{keywordflow}{else}}
\DoxyCodeLine{500           th\_b(k) = h\_tr(k) * t0(k) + kddt\_h(k+1) * te(k+1)}
\DoxyCodeLine{501           sh\_b(k) = h\_tr(k) * s0(k) + kddt\_h(k+1) * se(k+1)}
\DoxyCodeLine{502 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{503 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{504 }
\DoxyCodeLine{505       \textcolor{comment}{! Find the energy change due to a guess at the strength of diffusion at interface K.}}
\DoxyCodeLine{506       kddt\_h\_guess = kddt\_h(k)}
\DoxyCodeLine{507 }
\DoxyCodeLine{508       \textcolor{keywordflow}{if} (old\_pe\_calc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{509         \textcolor{keyword}{call }find\_pe\_chg\_orig(kddt\_h\_guess, h\_tr(k-\/1), hp\_b(k), \&}
\DoxyCodeLine{510                          dte\_term, dse\_term, dt\_k\_t2, ds\_k\_t2, \&}
\DoxyCodeLine{511                          dt\_to\_dpe(k-\/1), ds\_to\_dpe(k-\/1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), \&}
\DoxyCodeLine{512                          pres\_z(k), dt\_to\_dcolht(k-\/1), ds\_to\_dcolht(k-\/1), \&}
\DoxyCodeLine{513                          dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), \&}
\DoxyCodeLine{514                          pe\_chg=pe\_chg\_k(k,2), dpec\_dkd=dpeb\_dkd(k))}
\DoxyCodeLine{515       \textcolor{keywordflow}{else}}
\DoxyCodeLine{516         \textcolor{keyword}{call }find\_pe\_chg(0.0, kddt\_h\_guess, hp\_a(k-\/1), hp\_b(k), \&}
\DoxyCodeLine{517                          th\_a(k-\/1), sh\_a(k-\/1), th\_b(k), sh\_b(k), \&}
\DoxyCodeLine{518                          dt\_to\_dpe\_a(k-\/1), ds\_to\_dpe\_a(k-\/1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), \&}
\DoxyCodeLine{519                          pres\_z(k), dt\_to\_dcolht\_a(k-\/1), ds\_to\_dcolht\_a(k-\/1), \&}
\DoxyCodeLine{520                          dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), \&}
\DoxyCodeLine{521                          pe\_chg=pe\_chg\_k(k,2), dpec\_dkd=dpeb\_dkd(k), \&}
\DoxyCodeLine{522                          colht\_cor=colht\_cor\_k(k,2))}
\DoxyCodeLine{523 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{524 }
\DoxyCodeLine{525       \textcolor{keywordflow}{if} (debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{526         \textcolor{comment}{! Compare with a 4th-\/order finite difference estimate.}}
\DoxyCodeLine{527         \textcolor{keywordflow}{do} itt=1,5}
\DoxyCodeLine{528           kddt\_h\_guess = (1.0+0.01*(itt-\/3))*kddt\_h(k)}
\DoxyCodeLine{529 }
\DoxyCodeLine{530           \textcolor{keywordflow}{if} (old\_pe\_calc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{531             \textcolor{keyword}{call }find\_pe\_chg\_orig(kddt\_h\_guess, h\_tr(k-\/1), hp\_b(k), \&}
\DoxyCodeLine{532                            dte\_term, dse\_term, dt\_k\_t2, ds\_k\_t2, \&}
\DoxyCodeLine{533                            dt\_to\_dpe(k-\/1), ds\_to\_dpe(k-\/1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), \&}
\DoxyCodeLine{534                            pres\_z(k), dt\_to\_dcolht(k-\/1), ds\_to\_dcolht(k-\/1), \&}
\DoxyCodeLine{535                            dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), \&}
\DoxyCodeLine{536                            pe\_chg=pe\_chg(itt))}
\DoxyCodeLine{537           \textcolor{keywordflow}{else}}
\DoxyCodeLine{538             \textcolor{keyword}{call }find\_pe\_chg(0.0, kddt\_h\_guess, hp\_a(k-\/1), hp\_b(k), \&}
\DoxyCodeLine{539                              th\_a(k-\/1), sh\_a(k-\/1), th\_b(k), sh\_b(k), \&}
\DoxyCodeLine{540                              dt\_to\_dpe\_a(k-\/1), ds\_to\_dpe\_a(k-\/1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), \&}
\DoxyCodeLine{541                              pres\_z(k), dt\_to\_dcolht\_a(k-\/1), ds\_to\_dcolht\_a(k-\/1), \&}
\DoxyCodeLine{542                              dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), \&}
\DoxyCodeLine{543                              pe\_chg=pe\_chg(itt))}
\DoxyCodeLine{544 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{545 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{546 }
\DoxyCodeLine{547         dpeb\_dkd\_est(k) = (4.0*(pe\_chg(4)-\/pe\_chg(2))/(0.02*kddt\_h(k)) -\/ \&}
\DoxyCodeLine{548                                (pe\_chg(5)-\/pe\_chg(1))/(0.04*kddt\_h(k))) / 3.0}
\DoxyCodeLine{549         dpeb\_dkd\_trunc(k) = (pe\_chg(4)-\/pe\_chg(2))/(0.02*kddt\_h(k)) -\/ \&}
\DoxyCodeLine{550                             (pe\_chg(5)-\/pe\_chg(1))/(0.04*kddt\_h(k))}
\DoxyCodeLine{551         dpeb\_dkd\_err(k) = (dpeb\_dkd\_est(k) -\/ dpeb\_dkd(k))}
\DoxyCodeLine{552         dpeb\_dkd\_err\_norm(k) = (dpeb\_dkd\_est(k) -\/ dpeb\_dkd(k)) / \&}
\DoxyCodeLine{553                               (abs(dpeb\_dkd\_est(k)) + abs(dpeb\_dkd(k)) + 1e-\/100*us\%RZ\_to\_kg\_m2*us\%L\_T\_to\_m\_s**2)}
\DoxyCodeLine{554 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{555 }
\DoxyCodeLine{556       \textcolor{comment}{!   At this point, the final value of Kddt\_h(K) is known, so the estimated}}
\DoxyCodeLine{557       \textcolor{comment}{! properties for layer k can be calculated.}}
\DoxyCodeLine{558 }
\DoxyCodeLine{559       b1 = 1.0 / (hp\_b(k) + kddt\_h(k))}
\DoxyCodeLine{560       c1\_b(k) = kddt\_h(k) * b1}
\DoxyCodeLine{561       \textcolor{keywordflow}{if} (k==nz) \textcolor{keywordflow}{then}}
\DoxyCodeLine{562         te(nz) = b1* (h\_tr(nz)*t0(nz))}
\DoxyCodeLine{563         se(nz) = b1* (h\_tr(nz)*s0(nz))}
\DoxyCodeLine{564       \textcolor{keywordflow}{else}}
\DoxyCodeLine{565         te(k) = b1 * (h\_tr(k) * t0(k) + kddt\_h(k+1) * te(k+1))}
\DoxyCodeLine{566         se(k) = b1 * (h\_tr(k) * s0(k) + kddt\_h(k+1) * se(k+1))}
\DoxyCodeLine{567 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{568       \textcolor{keywordflow}{if} (old\_pe\_calc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{569         dte(k) = b1 * ( kddt\_h(k)*(t0(k-\/1)-\/t0(k)) + dte\_t2 )}
\DoxyCodeLine{570         dse(k) = b1 * ( kddt\_h(k)*(s0(k-\/1)-\/s0(k)) + dse\_t2 )}
\DoxyCodeLine{571 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{572 }
\DoxyCodeLine{573       hp\_b(k-\/1) = h\_tr(k-\/1) + (hp\_b(k) * b1) * kddt\_h(k)}
\DoxyCodeLine{574       dt\_to\_dpe\_b(k-\/1) = dt\_to\_dpe(k-\/1) + c1\_b(k)*dt\_to\_dpe\_b(k)}
\DoxyCodeLine{575       ds\_to\_dpe\_b(k-\/1) = ds\_to\_dpe(k-\/1) + c1\_b(k)*ds\_to\_dpe\_b(k)}
\DoxyCodeLine{576       dt\_to\_dcolht\_b(k-\/1) = dt\_to\_dcolht(k-\/1) + c1\_b(k)*dt\_to\_dcolht\_b(k)}
\DoxyCodeLine{577       ds\_to\_dcolht\_b(k-\/1) = ds\_to\_dcolht(k-\/1) + c1\_b(k)*ds\_to\_dcolht\_b(k)}
\DoxyCodeLine{578 }
\DoxyCodeLine{579 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{580 }
\DoxyCodeLine{581     b1 = 1.0 / (hp\_b(1))}
\DoxyCodeLine{582     tf(1) = b1 * (h\_tr(1) * t0(1) + kddt\_h(2) * te(2))}
\DoxyCodeLine{583     sf(1) = b1 * (h\_tr(1) * s0(1) + kddt\_h(2) * se(2))}
\DoxyCodeLine{584     \textcolor{keywordflow}{if} (old\_pe\_calc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{585       dte(1) = b1 * kddt\_h(2) * ((t0(2)-\/t0(1)) + dte(2))}
\DoxyCodeLine{586       dse(1) = b1 * kddt\_h(2) * ((s0(2)-\/s0(1)) + dse(2))}
\DoxyCodeLine{587 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{588 }
\DoxyCodeLine{589     \textcolor{keywordflow}{do} k=2,nz}
\DoxyCodeLine{590       tf(k) = te(k) + c1\_b(k)*tf(k-\/1)}
\DoxyCodeLine{591       sf(k) = se(k) + c1\_b(k)*sf(k-\/1)}
\DoxyCodeLine{592 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{593 }
\DoxyCodeLine{594     \textcolor{keywordflow}{if} (debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{595       \textcolor{keywordflow}{do} k=1,nz ; tb(k) = tf(k) ; sb(k) = sf(k) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{596       pe\_chg\_tot1b = 0.0 ; pe\_chg\_tot2b = 0.0 ; t\_chg\_totb = 0.0}
\DoxyCodeLine{597       \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{598         pe\_chg\_tot1b = pe\_chg\_tot1b + (dt\_to\_dpe(k) * (tf(k) -\/ t0(k)) + \&}
\DoxyCodeLine{599                                        ds\_to\_dpe(k) * (sf(k) -\/ s0(k)))}
\DoxyCodeLine{600         t\_chg\_totb = t\_chg\_totb + h\_tr(k) * (tf(k) -\/ t0(k))}
\DoxyCodeLine{601 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{602       \textcolor{keywordflow}{do} k=2,nz}
\DoxyCodeLine{603         pe\_chg\_tot2b = pe\_chg\_tot2b + (pe\_chg\_k(k,2) -\/ colht\_cor\_k(k,2))}
\DoxyCodeLine{604 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{605 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{606 }
\DoxyCodeLine{607 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{608 }
\DoxyCodeLine{609   \textcolor{keywordflow}{if} (central) \textcolor{keywordflow}{then}}
\DoxyCodeLine{610 }
\DoxyCodeLine{611     \textcolor{comment}{! Set up values appropriate for no diffusivity.}}
\DoxyCodeLine{612     \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{613       hp\_a(k) = h\_tr(k) ; hp\_b(k) = h\_tr(k)}
\DoxyCodeLine{614       dt\_to\_dpe\_a(k) = dt\_to\_dpe(k) ; ds\_to\_dpe\_a(k) = ds\_to\_dpe(k)}
\DoxyCodeLine{615       dt\_to\_dpe\_b(k) = dt\_to\_dpe(k) ; ds\_to\_dpe\_b(k) = ds\_to\_dpe(k)}
\DoxyCodeLine{616       dt\_to\_dcolht\_a(k) = dt\_to\_dcolht(k) ; ds\_to\_dcolht\_a(k) = ds\_to\_dcolht(k)}
\DoxyCodeLine{617       dt\_to\_dcolht\_b(k) = dt\_to\_dcolht(k) ; ds\_to\_dcolht\_b(k) = ds\_to\_dcolht(k)}
\DoxyCodeLine{618 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{619 }
\DoxyCodeLine{620     \textcolor{comment}{! Calculate the dependencies on layers above.}}
\DoxyCodeLine{621     kddt\_h\_a(1) = 0.0}
\DoxyCodeLine{622     \textcolor{keywordflow}{do} k=2,nz  \textcolor{comment}{! Loop over interior interfaces.}}
\DoxyCodeLine{623       \textcolor{comment}{! First calculate some terms that are independent of the change in Kddt\_h(K).}}
\DoxyCodeLine{624       kd0 = 0.0  \textcolor{comment}{! This might need to be changed -\/ it is the already applied value of Kddt\_h(K).}}
\DoxyCodeLine{625       \textcolor{keywordflow}{if} (k<=2) \textcolor{keywordflow}{then}}
\DoxyCodeLine{626         th\_a(k-\/1) = h\_tr(k-\/1) * t0(k-\/1) ; sh\_a(k-\/1) = h\_tr(k-\/1) * s0(k-\/1)}
\DoxyCodeLine{627       \textcolor{keywordflow}{else}}
\DoxyCodeLine{628         th\_a(k-\/1) = h\_tr(k-\/1) * t0(k-\/1) + kddt\_h(k-\/1) * te\_a(k-\/2)}
\DoxyCodeLine{629         sh\_a(k-\/1) = h\_tr(k-\/1) * s0(k-\/1) + kddt\_h(k-\/1) * se\_a(k-\/2)}
\DoxyCodeLine{630 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{631       th\_b(k) = h\_tr(k) * t0(k) ; sh\_b(k) = h\_tr(k) * s0(k)}
\DoxyCodeLine{632 }
\DoxyCodeLine{633       kddt\_h\_a(k) = 0.0 ; \textcolor{keywordflow}{if} (k < k\_cent) kddt\_h\_a(k) = kddt\_h(k)}
\DoxyCodeLine{634       dkd = kddt\_h\_a(k)}
\DoxyCodeLine{635 }
\DoxyCodeLine{636       \textcolor{keyword}{call }find\_pe\_chg(kd0, dkd, hp\_a(k-\/1), hp\_b(k), \&}
\DoxyCodeLine{637                        th\_a(k-\/1), sh\_a(k-\/1), th\_b(k), sh\_b(k), \&}
\DoxyCodeLine{638                        dt\_to\_dpe\_a(k-\/1), ds\_to\_dpe\_a(k-\/1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), \&}
\DoxyCodeLine{639                        pres\_z(k), dt\_to\_dcolht\_a(k-\/1), ds\_to\_dcolht\_a(k-\/1), \&}
\DoxyCodeLine{640                        dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), \&}
\DoxyCodeLine{641                        pe\_chg=pe\_change, colht\_cor=colht\_cor)}
\DoxyCodeLine{642       pe\_chg\_k(k,3) = pe\_change}
\DoxyCodeLine{643       colht\_cor\_k(k,3) = colht\_cor}
\DoxyCodeLine{644 }
\DoxyCodeLine{645       b1 = 1.0 / (hp\_a(k-\/1) + kddt\_h\_a(k))}
\DoxyCodeLine{646       c1\_a(k) = kddt\_h\_a(k) * b1}
\DoxyCodeLine{647       \textcolor{keywordflow}{if} (k==2) \textcolor{keywordflow}{then}}
\DoxyCodeLine{648         te\_a(1) = b1*(h\_tr(1)*t0(1))}
\DoxyCodeLine{649         se\_a(1) = b1*(h\_tr(1)*s0(1))}
\DoxyCodeLine{650       \textcolor{keywordflow}{else}}
\DoxyCodeLine{651         te\_a(k-\/1) = b1 * (h\_tr(k-\/1) * t0(k-\/1) + kddt\_h\_a(k-\/1) * te\_a(k-\/2))}
\DoxyCodeLine{652         se\_a(k-\/1) = b1 * (h\_tr(k-\/1) * s0(k-\/1) + kddt\_h\_a(k-\/1) * se\_a(k-\/2))}
\DoxyCodeLine{653 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{654 }
\DoxyCodeLine{655       hp\_a(k) = h\_tr(k) + (hp\_a(k-\/1) * b1) * kddt\_h\_a(k)}
\DoxyCodeLine{656       dt\_to\_dpe\_a(k) = dt\_to\_dpe(k) + c1\_a(k)*dt\_to\_dpe\_a(k-\/1)}
\DoxyCodeLine{657       ds\_to\_dpe\_a(k) = ds\_to\_dpe(k) + c1\_a(k)*ds\_to\_dpe\_a(k-\/1)}
\DoxyCodeLine{658       dt\_to\_dcolht\_a(k) = dt\_to\_dcolht(k) + c1\_a(k)*dt\_to\_dcolht\_a(k-\/1)}
\DoxyCodeLine{659       ds\_to\_dcolht\_a(k) = ds\_to\_dcolht(k) + c1\_a(k)*ds\_to\_dcolht\_a(k-\/1)}
\DoxyCodeLine{660 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{661 }
\DoxyCodeLine{662     \textcolor{comment}{! Calculate the dependencies on layers below.}}
\DoxyCodeLine{663     kddt\_h\_b(nz+1) = 0.0}
\DoxyCodeLine{664     \textcolor{keywordflow}{do} k=nz,2,-\/1  \textcolor{comment}{! Loop over interior interfaces.}}
\DoxyCodeLine{665       \textcolor{comment}{! First calculate some terms that are independent of the change in Kddt\_h(K).}}
\DoxyCodeLine{666       kd0 = 0.0  \textcolor{comment}{! This might need to be changed -\/ it is the already applied value of Kddt\_h(K).}}
\DoxyCodeLine{667 \textcolor{comment}{!     if (K<=2) then}}
\DoxyCodeLine{668         th\_a(k-\/1) = h\_tr(k-\/1) * t0(k-\/1) ; sh\_a(k-\/1) = h\_tr(k-\/1) * s0(k-\/1)}
\DoxyCodeLine{669 \textcolor{comment}{!     else}}
\DoxyCodeLine{670 \textcolor{comment}{!       Th\_a(k-\/1) = h\_tr(k-\/1) * T0(k-\/1) + Kddt\_h(K-\/1) * Te\_a(k-\/2)}}
\DoxyCodeLine{671 \textcolor{comment}{!       Sh\_a(k-\/1) = h\_tr(k-\/1) * S0(k-\/1) + Kddt\_h(K-\/1) * Se\_a(k-\/2)}}
\DoxyCodeLine{672 \textcolor{comment}{!     endif}}
\DoxyCodeLine{673       \textcolor{keywordflow}{if} (k>=nz) \textcolor{keywordflow}{then}}
\DoxyCodeLine{674         th\_b(k) = h\_tr(k) * t0(k) ; sh\_b(k) = h\_tr(k) * s0(k)}
\DoxyCodeLine{675       \textcolor{keywordflow}{else}}
\DoxyCodeLine{676         th\_b(k) = h\_tr(k) * t0(k) + kddt\_h(k+1) * te\_b(k+1)}
\DoxyCodeLine{677         sh\_b(k) = h\_tr(k) * s0(k) + kddt\_h(k+1) * se\_b(k+1)}
\DoxyCodeLine{678 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{679 }
\DoxyCodeLine{680       kddt\_h\_b(k) = 0.0 ; \textcolor{keywordflow}{if} (k > k\_cent) kddt\_h\_b(k) = kddt\_h(k)}
\DoxyCodeLine{681       dkd = kddt\_h\_b(k)}
\DoxyCodeLine{682 }
\DoxyCodeLine{683       \textcolor{keyword}{call }find\_pe\_chg(kd0, dkd, hp\_a(k-\/1), hp\_b(k), \&}
\DoxyCodeLine{684                        th\_a(k-\/1), sh\_a(k-\/1), th\_b(k), sh\_b(k), \&}
\DoxyCodeLine{685                        dt\_to\_dpe\_a(k-\/1), ds\_to\_dpe\_a(k-\/1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), \&}
\DoxyCodeLine{686                        pres\_z(k), dt\_to\_dcolht\_a(k-\/1), ds\_to\_dcolht\_a(k-\/1), \&}
\DoxyCodeLine{687                        dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), \&}
\DoxyCodeLine{688                        pe\_chg=pe\_change, colht\_cor=colht\_cor)}
\DoxyCodeLine{689       pe\_chg\_k(k,3) = pe\_chg\_k(k,3) + pe\_change}
\DoxyCodeLine{690       colht\_cor\_k(k,3) = colht\_cor\_k(k,3) + colht\_cor}
\DoxyCodeLine{691 }
\DoxyCodeLine{692       b1 = 1.0 / (hp\_b(k) + kddt\_h\_b(k))}
\DoxyCodeLine{693       c1\_b(k) = kddt\_h\_b(k) * b1}
\DoxyCodeLine{694       \textcolor{keywordflow}{if} (k==nz) \textcolor{keywordflow}{then}}
\DoxyCodeLine{695         te\_b(k) = b1 * (h\_tr(k)*t0(k))}
\DoxyCodeLine{696         se\_b(k) = b1 * (h\_tr(k)*s0(k))}
\DoxyCodeLine{697       \textcolor{keywordflow}{else}}
\DoxyCodeLine{698         te\_b(k) = b1 * (h\_tr(k) * t0(k) + kddt\_h\_b(k+1) * te\_b(k+1))}
\DoxyCodeLine{699         se\_b(k) = b1 * (h\_tr(k) * s0(k) + kddt\_h\_b(k+1) * se\_b(k+1))}
\DoxyCodeLine{700 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{701 }
\DoxyCodeLine{702       hp\_b(k-\/1) = h\_tr(k-\/1) + (hp\_b(k) * b1) * kddt\_h\_b(k)}
\DoxyCodeLine{703       dt\_to\_dpe\_b(k-\/1) = dt\_to\_dpe(k-\/1) + c1\_b(k)*dt\_to\_dpe\_b(k)}
\DoxyCodeLine{704       ds\_to\_dpe\_b(k-\/1) = ds\_to\_dpe(k-\/1) + c1\_b(k)*ds\_to\_dpe\_b(k)}
\DoxyCodeLine{705       dt\_to\_dcolht\_b(k-\/1) = dt\_to\_dcolht(k-\/1) + c1\_b(k)*dt\_to\_dcolht\_b(k)}
\DoxyCodeLine{706       ds\_to\_dcolht\_b(k-\/1) = ds\_to\_dcolht(k-\/1) + c1\_b(k)*ds\_to\_dcolht\_b(k)}
\DoxyCodeLine{707 }
\DoxyCodeLine{708 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{709 }
\DoxyCodeLine{710     \textcolor{comment}{! Calculate the final solution for the layers surrounding interface K\_cent}}
\DoxyCodeLine{711     k = k\_cent}
\DoxyCodeLine{712 }
\DoxyCodeLine{713     \textcolor{comment}{! First calculate some terms that are independent of the change in Kddt\_h(K).}}
\DoxyCodeLine{714     kd0 = 0.0  \textcolor{comment}{! This might need to be changed -\/ it is the already applied value of Kddt\_h(K).}}
\DoxyCodeLine{715     \textcolor{keywordflow}{if} (k<=2) \textcolor{keywordflow}{then}}
\DoxyCodeLine{716       th\_a(k-\/1) = h\_tr(k-\/1) * t0(k-\/1) ; sh\_a(k-\/1) = h\_tr(k-\/1) * s0(k-\/1)}
\DoxyCodeLine{717     \textcolor{keywordflow}{else}}
\DoxyCodeLine{718       th\_a(k-\/1) = h\_tr(k-\/1) * t0(k-\/1) + kddt\_h(k-\/1) * te\_a(k-\/2)}
\DoxyCodeLine{719       sh\_a(k-\/1) = h\_tr(k-\/1) * s0(k-\/1) + kddt\_h(k-\/1) * se\_a(k-\/2)}
\DoxyCodeLine{720 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{721     \textcolor{keywordflow}{if} (k>=nz) \textcolor{keywordflow}{then}}
\DoxyCodeLine{722       th\_b(k) = h\_tr(k) * t0(k) ; sh\_b(k) = h\_tr(k) * s0(k)}
\DoxyCodeLine{723     \textcolor{keywordflow}{else}}
\DoxyCodeLine{724       th\_b(k) = h\_tr(k) * t0(k) + kddt\_h(k+1) * te\_b(k+1)}
\DoxyCodeLine{725       sh\_b(k) = h\_tr(k) * s0(k) + kddt\_h(k+1) * se\_b(k+1)}
\DoxyCodeLine{726 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{727 }
\DoxyCodeLine{728     dkd = kddt\_h(k)}
\DoxyCodeLine{729 }
\DoxyCodeLine{730     \textcolor{keyword}{call }find\_pe\_chg(kd0, dkd, hp\_a(k-\/1), hp\_b(k), \&}
\DoxyCodeLine{731                      th\_a(k-\/1), sh\_a(k-\/1), th\_b(k), sh\_b(k), \&}
\DoxyCodeLine{732                      dt\_to\_dpe\_a(k-\/1), ds\_to\_dpe\_a(k-\/1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), \&}
\DoxyCodeLine{733                      pres\_z(k), dt\_to\_dcolht\_a(k-\/1), ds\_to\_dcolht\_a(k-\/1), \&}
\DoxyCodeLine{734                      dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), \&}
\DoxyCodeLine{735                      pe\_chg=pe\_change, colht\_cor=colht\_cor)}
\DoxyCodeLine{736     pe\_chg\_k(k,3) = pe\_chg\_k(k,3) + pe\_change}
\DoxyCodeLine{737     colht\_cor\_k(k,3) = colht\_cor\_k(k,3) + colht\_cor}
\DoxyCodeLine{738 }
\DoxyCodeLine{739 }
\DoxyCodeLine{740     \textcolor{comment}{!   To derive the following, first to a partial update for the estimated}}
\DoxyCodeLine{741     \textcolor{comment}{! temperatures and salinities in the layers around this interface:}}
\DoxyCodeLine{742     \textcolor{comment}{!   b1\_a = 1.0 / (hp\_a(k-\/1) + Kddt\_h(K))}}
\DoxyCodeLine{743     \textcolor{comment}{!   b1\_b = 1.0 / (hp\_b(k) + Kddt\_h(K))}}
\DoxyCodeLine{744     \textcolor{comment}{!   Te\_up(k) = Th\_b(k) * b1\_b ; Se\_up(k) = Sh\_b(k) * b1\_b}}
\DoxyCodeLine{745     \textcolor{comment}{!   Te\_up(k-\/1) = Th\_a(k-\/1) * b1\_a ; Se\_up(k-\/1) = Sh\_a(k-\/1) * b1\_a}}
\DoxyCodeLine{746     \textcolor{comment}{! Find the final values of T \& S for both layers around K\_cent, using that}}
\DoxyCodeLine{747     \textcolor{comment}{!   c1\_a(K) = Kddt\_h(K) * b1\_a ; c1\_b(K) = Kddt\_h(K) * b1\_b}}
\DoxyCodeLine{748     \textcolor{comment}{!   Tf(K\_cent-\/1) = Te\_up(K\_cent-\/1) + c1\_a(K\_cent)*Tf(K\_cent)}}
\DoxyCodeLine{749     \textcolor{comment}{!   Tf(K\_cent)   = Te\_up(K\_cent) + c1\_b(K\_cent)*Tf(K\_cent-\/1)}}
\DoxyCodeLine{750     \textcolor{comment}{! but further exploiting the expressions for c1\_a and c1\_b to avoid}}
\DoxyCodeLine{751     \textcolor{comment}{! subtraction in the denominator, and use only a single division.}}
\DoxyCodeLine{752     b1 = 1.0 / (hp\_a(k-\/1)*hp\_b(k) + kddt\_h(k)*(hp\_a(k-\/1) + hp\_b(k)))}
\DoxyCodeLine{753     tf(k-\/1) = ((hp\_b(k) + kddt\_h(k)) * th\_a(k-\/1) + kddt\_h(k) * th\_b(k) ) * b1}
\DoxyCodeLine{754     sf(k-\/1) = ((hp\_b(k) + kddt\_h(k)) * sh\_a(k-\/1) + kddt\_h(k) * sh\_b(k) ) * b1}
\DoxyCodeLine{755     tf(k) = (kddt\_h(k) * th\_a(k-\/1) + (hp\_a(k-\/1) + kddt\_h(k)) * th\_b(k) ) * b1}
\DoxyCodeLine{756     sf(k) = (kddt\_h(k) * sh\_a(k-\/1) + (hp\_a(k-\/1) + kddt\_h(k)) * sh\_b(k) ) * b1}
\DoxyCodeLine{757 }
\DoxyCodeLine{758     c1\_a(k) = kddt\_h(k) / (hp\_a(k-\/1) + kddt\_h(k))}
\DoxyCodeLine{759     c1\_b(k) = kddt\_h(k) / (hp\_b(k) + kddt\_h(k))}
\DoxyCodeLine{760 }
\DoxyCodeLine{761     \textcolor{comment}{! Now update the other layer working outward from k\_cent to determine the final}}
\DoxyCodeLine{762     \textcolor{comment}{! temperatures and salinities.}}
\DoxyCodeLine{763     \textcolor{keywordflow}{do} k=k\_cent-\/2,1,-\/1}
\DoxyCodeLine{764       tf(k) = te\_a(k) + c1\_a(k+1)*tf(k+1)}
\DoxyCodeLine{765       sf(k) = se\_a(k) + c1\_a(k+1)*sf(k+1)}
\DoxyCodeLine{766 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{767     \textcolor{keywordflow}{do} k=k\_cent+1,nz}
\DoxyCodeLine{768       tf(k) = te\_b(k) + c1\_b(k)*tf(k-\/1)}
\DoxyCodeLine{769       sf(k) = se\_b(k) + c1\_b(k)*sf(k-\/1)}
\DoxyCodeLine{770 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{771 }
\DoxyCodeLine{772     \textcolor{keywordflow}{if} (debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{773       pe\_chg\_tot1c = 0.0 ; pe\_chg\_tot2c = 0.0 ; t\_chg\_totc = 0.0}
\DoxyCodeLine{774       \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{775         pe\_chg\_tot1c = pe\_chg\_tot1c + (dt\_to\_dpe(k) * (tf(k) -\/ t0(k)) + \&}
\DoxyCodeLine{776                                        ds\_to\_dpe(k) * (sf(k) -\/ s0(k)))}
\DoxyCodeLine{777         t\_chg\_totc = t\_chg\_totc + h\_tr(k) * (tf(k) -\/ t0(k))}
\DoxyCodeLine{778 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{779       \textcolor{keywordflow}{do} k=2,nz}
\DoxyCodeLine{780         pe\_chg\_tot2c = pe\_chg\_tot2c + (pe\_chg\_k(k,3) -\/ colht\_cor\_k(k,3))}
\DoxyCodeLine{781 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{782 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{783 }
\DoxyCodeLine{784 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{785 }
\DoxyCodeLine{786   \textcolor{keywordflow}{if} (halves) \textcolor{keywordflow}{then}}
\DoxyCodeLine{787 }
\DoxyCodeLine{788     \textcolor{comment}{! Set up values appropriate for no diffusivity.}}
\DoxyCodeLine{789     \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{790       hp\_a(k) = h\_tr(k) ; hp\_b(k) = h\_tr(k)}
\DoxyCodeLine{791       dt\_to\_dpe\_a(k) = dt\_to\_dpe(k) ; ds\_to\_dpe\_a(k) = ds\_to\_dpe(k)}
\DoxyCodeLine{792       dt\_to\_dpe\_b(k) = dt\_to\_dpe(k) ; ds\_to\_dpe\_b(k) = ds\_to\_dpe(k)}
\DoxyCodeLine{793       dt\_to\_dcolht\_a(k) = dt\_to\_dcolht(k) ; ds\_to\_dcolht\_a(k) = ds\_to\_dcolht(k)}
\DoxyCodeLine{794       dt\_to\_dcolht\_b(k) = dt\_to\_dcolht(k) ; ds\_to\_dcolht\_b(k) = ds\_to\_dcolht(k)}
\DoxyCodeLine{795 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{796     \textcolor{keywordflow}{do} k=1,nz+1}
\DoxyCodeLine{797       kd\_so\_far(k) = 0.0}
\DoxyCodeLine{798 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{799 }
\DoxyCodeLine{800     \textcolor{comment}{! Calculate the dependencies on layers above.}}
\DoxyCodeLine{801     kddt\_h\_a(1) = 0.0}
\DoxyCodeLine{802     \textcolor{keywordflow}{do} k=2,nz  \textcolor{comment}{! Loop over interior interfaces.}}
\DoxyCodeLine{803       \textcolor{comment}{! First calculate some terms that are independent of the change in Kddt\_h(K).}}
\DoxyCodeLine{804       kd0 = kd\_so\_far(k)}
\DoxyCodeLine{805       \textcolor{keywordflow}{if} (k<=2) \textcolor{keywordflow}{then}}
\DoxyCodeLine{806         th\_a(k-\/1) = h\_tr(k-\/1) * t0(k-\/1) ; sh\_a(k-\/1) = h\_tr(k-\/1) * s0(k-\/1)}
\DoxyCodeLine{807       \textcolor{keywordflow}{else}}
\DoxyCodeLine{808         th\_a(k-\/1) = h\_tr(k-\/1) * t0(k-\/1) + kd\_so\_far(k-\/1) * te(k-\/2)}
\DoxyCodeLine{809         sh\_a(k-\/1) = h\_tr(k-\/1) * s0(k-\/1) + kd\_so\_far(k-\/1) * se(k-\/2)}
\DoxyCodeLine{810 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{811       th\_b(k) = h\_tr(k) * t0(k) ; sh\_b(k) = h\_tr(k) * s0(k)}
\DoxyCodeLine{812 }
\DoxyCodeLine{813       dkd = 0.5 * kddt\_h(k) -\/ kd\_so\_far(k)}
\DoxyCodeLine{814 }
\DoxyCodeLine{815       \textcolor{keyword}{call }find\_pe\_chg(kd0, dkd, hp\_a(k-\/1), hp\_b(k), \&}
\DoxyCodeLine{816                        th\_a(k-\/1), sh\_a(k-\/1), th\_b(k), sh\_b(k), \&}
\DoxyCodeLine{817                        dt\_to\_dpe\_a(k-\/1), ds\_to\_dpe\_a(k-\/1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), \&}
\DoxyCodeLine{818                        pres\_z(k), dt\_to\_dcolht\_a(k-\/1), ds\_to\_dcolht\_a(k-\/1), \&}
\DoxyCodeLine{819                        dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), \&}
\DoxyCodeLine{820                        pe\_chg=pe\_change, colht\_cor=colht\_cor)}
\DoxyCodeLine{821 }
\DoxyCodeLine{822       pe\_chg\_k(k,4) = pe\_change}
\DoxyCodeLine{823       colht\_cor\_k(k,4) = colht\_cor}
\DoxyCodeLine{824 }
\DoxyCodeLine{825       kd\_so\_far(k) = kd\_so\_far(k) + dkd}
\DoxyCodeLine{826 }
\DoxyCodeLine{827       b1 = 1.0 / (hp\_a(k-\/1) + kd\_so\_far(k))}
\DoxyCodeLine{828       c1\_a(k) = kd\_so\_far(k) * b1}
\DoxyCodeLine{829       \textcolor{keywordflow}{if} (k==2) \textcolor{keywordflow}{then}}
\DoxyCodeLine{830         te(1) = b1*(h\_tr(1)*t0(1))}
\DoxyCodeLine{831         se(1) = b1*(h\_tr(1)*s0(1))}
\DoxyCodeLine{832       \textcolor{keywordflow}{else}}
\DoxyCodeLine{833         te(k-\/1) = b1 * (h\_tr(k-\/1) * t0(k-\/1) + kd\_so\_far(k-\/1) * te(k-\/2))}
\DoxyCodeLine{834         se(k-\/1) = b1 * (h\_tr(k-\/1) * s0(k-\/1) + kd\_so\_far(k-\/1) * se(k-\/2))}
\DoxyCodeLine{835 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{836 }
\DoxyCodeLine{837       hp\_a(k) = h\_tr(k) + (hp\_a(k-\/1) * b1) * kd\_so\_far(k)}
\DoxyCodeLine{838       dt\_to\_dpe\_a(k) = dt\_to\_dpe(k) + c1\_a(k)*dt\_to\_dpe\_a(k-\/1)}
\DoxyCodeLine{839       ds\_to\_dpe\_a(k) = ds\_to\_dpe(k) + c1\_a(k)*ds\_to\_dpe\_a(k-\/1)}
\DoxyCodeLine{840       dt\_to\_dcolht\_a(k) = dt\_to\_dcolht(k) + c1\_a(k)*dt\_to\_dcolht\_a(k-\/1)}
\DoxyCodeLine{841       ds\_to\_dcolht\_a(k) = ds\_to\_dcolht(k) + c1\_a(k)*ds\_to\_dcolht\_a(k-\/1)}
\DoxyCodeLine{842 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{843 }
\DoxyCodeLine{844     \textcolor{comment}{! Calculate the dependencies on layers below.}}
\DoxyCodeLine{845     \textcolor{keywordflow}{do} k=nz,2,-\/1  \textcolor{comment}{! Loop over interior interfaces.}}
\DoxyCodeLine{846       \textcolor{comment}{! First calculate some terms that are independent of the change in Kddt\_h(K).}}
\DoxyCodeLine{847       kd0 = kd\_so\_far(k)}
\DoxyCodeLine{848       \textcolor{keywordflow}{if} (k<=2) \textcolor{keywordflow}{then}}
\DoxyCodeLine{849         th\_a(k-\/1) = h\_tr(k-\/1) * t0(k-\/1) ; sh\_a(k-\/1) = h\_tr(k-\/1) * s0(k-\/1)}
\DoxyCodeLine{850       \textcolor{keywordflow}{else}}
\DoxyCodeLine{851         th\_a(k-\/1) = h\_tr(k-\/1) * t0(k-\/1) + kd\_so\_far(k-\/1) * te(k-\/2)}
\DoxyCodeLine{852         sh\_a(k-\/1) = h\_tr(k-\/1) * s0(k-\/1) + kd\_so\_far(k-\/1) * se(k-\/2)}
\DoxyCodeLine{853 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{854       \textcolor{keywordflow}{if} (k>=nz) \textcolor{keywordflow}{then}}
\DoxyCodeLine{855         th\_b(k) = h\_tr(k) * t0(k) ; sh\_b(k) = h\_tr(k) * s0(k)}
\DoxyCodeLine{856       \textcolor{keywordflow}{else}}
\DoxyCodeLine{857         th\_b(k) = h\_tr(k) * t0(k) + kd\_so\_far(k+1) * te(k+1)}
\DoxyCodeLine{858         sh\_b(k) = h\_tr(k) * s0(k) + kd\_so\_far(k+1) * se(k+1)}
\DoxyCodeLine{859 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{860 }
\DoxyCodeLine{861       dkd = kddt\_h(k) -\/ kd\_so\_far(k)}
\DoxyCodeLine{862 }
\DoxyCodeLine{863       \textcolor{keyword}{call }find\_pe\_chg(kd0, dkd, hp\_a(k-\/1), hp\_b(k), \&}
\DoxyCodeLine{864                        th\_a(k-\/1), sh\_a(k-\/1), th\_b(k), sh\_b(k), \&}
\DoxyCodeLine{865                        dt\_to\_dpe\_a(k-\/1), ds\_to\_dpe\_a(k-\/1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), \&}
\DoxyCodeLine{866                        pres\_z(k), dt\_to\_dcolht\_a(k-\/1), ds\_to\_dcolht\_a(k-\/1), \&}
\DoxyCodeLine{867                        dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), \&}
\DoxyCodeLine{868                        pe\_chg=pe\_change, colht\_cor=colht\_cor)}
\DoxyCodeLine{869 }
\DoxyCodeLine{870       pe\_chg\_k(k,4) = pe\_chg\_k(k,4) + pe\_change}
\DoxyCodeLine{871       colht\_cor\_k(k,4) = colht\_cor\_k(k,4) + colht\_cor}
\DoxyCodeLine{872 }
\DoxyCodeLine{873 }
\DoxyCodeLine{874       kd\_so\_far(k) = kd\_so\_far(k) + dkd}
\DoxyCodeLine{875 }
\DoxyCodeLine{876       b1 = 1.0 / (hp\_b(k) + kd\_so\_far(k))}
\DoxyCodeLine{877       c1\_b(k) = kd\_so\_far(k) * b1}
\DoxyCodeLine{878       \textcolor{keywordflow}{if} (k==nz) \textcolor{keywordflow}{then}}
\DoxyCodeLine{879         te(k) = b1 * (h\_tr(k)*t0(k))}
\DoxyCodeLine{880         se(k) = b1 * (h\_tr(k)*s0(k))}
\DoxyCodeLine{881       \textcolor{keywordflow}{else}}
\DoxyCodeLine{882         te(k) = b1 * (h\_tr(k) * t0(k) + kd\_so\_far(k+1) * te(k+1))}
\DoxyCodeLine{883         se(k) = b1 * (h\_tr(k) * s0(k) + kd\_so\_far(k+1) * se(k+1))}
\DoxyCodeLine{884 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{885 }
\DoxyCodeLine{886       hp\_b(k-\/1) = h\_tr(k-\/1) + (hp\_b(k) * b1) * kd\_so\_far(k)}
\DoxyCodeLine{887       dt\_to\_dpe\_b(k-\/1) = dt\_to\_dpe(k-\/1) + c1\_b(k)*dt\_to\_dpe\_b(k)}
\DoxyCodeLine{888       ds\_to\_dpe\_b(k-\/1) = ds\_to\_dpe(k-\/1) + c1\_b(k)*ds\_to\_dpe\_b(k)}
\DoxyCodeLine{889       dt\_to\_dcolht\_b(k-\/1) = dt\_to\_dcolht(k-\/1) + c1\_b(k)*dt\_to\_dcolht\_b(k)}
\DoxyCodeLine{890       ds\_to\_dcolht\_b(k-\/1) = ds\_to\_dcolht(k-\/1) + c1\_b(k)*ds\_to\_dcolht\_b(k)}
\DoxyCodeLine{891 }
\DoxyCodeLine{892 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{893 }
\DoxyCodeLine{894     \textcolor{comment}{! Now update the other layer working down from the top to determine the}}
\DoxyCodeLine{895     \textcolor{comment}{! final temperatures and salinities.}}
\DoxyCodeLine{896     b1 = 1.0 / (hp\_b(1))}
\DoxyCodeLine{897     tf(1) = b1 * (h\_tr(1) * t0(1) + kddt\_h(2) * te(2))}
\DoxyCodeLine{898     sf(1) = b1 * (h\_tr(1) * s0(1) + kddt\_h(2) * se(2))}
\DoxyCodeLine{899     \textcolor{keywordflow}{do} k=2,nz}
\DoxyCodeLine{900       tf(k) = te(k) + c1\_b(k)*tf(k-\/1)}
\DoxyCodeLine{901       sf(k) = se(k) + c1\_b(k)*sf(k-\/1)}
\DoxyCodeLine{902 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{903 }
\DoxyCodeLine{904     \textcolor{keywordflow}{if} (debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{905       pe\_chg\_tot1d = 0.0 ; pe\_chg\_tot2d = 0.0 ; t\_chg\_totd = 0.0}
\DoxyCodeLine{906       \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{907         pe\_chg\_tot1d = pe\_chg\_tot1d + (dt\_to\_dpe(k) * (tf(k) -\/ t0(k)) + \&}
\DoxyCodeLine{908                                        ds\_to\_dpe(k) * (sf(k) -\/ s0(k)))}
\DoxyCodeLine{909         t\_chg\_totd = t\_chg\_totd + h\_tr(k) * (tf(k) -\/ t0(k))}
\DoxyCodeLine{910 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{911       \textcolor{keywordflow}{do} k=2,nz}
\DoxyCodeLine{912         pe\_chg\_tot2d = pe\_chg\_tot2d + (pe\_chg\_k(k,4) -\/ colht\_cor\_k(k,4))}
\DoxyCodeLine{913 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{914 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{915 }
\DoxyCodeLine{916 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{917 }
\DoxyCodeLine{918   energy\_kd = 0.0 ; \textcolor{keywordflow}{do} k=2,nz ; energy\_kd = energy\_kd + pe\_chg\_k(k,1) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{919   energy\_kd = energy\_kd / dt}
\DoxyCodeLine{920 }
\DoxyCodeLine{921   \textcolor{keywordflow}{if} (do\_print) \textcolor{keywordflow}{then}}
\DoxyCodeLine{922     \textcolor{keywordflow}{if} (cs\%id\_ERt>0) \textcolor{keyword}{call }post\_data(cs\%id\_ERt, pe\_chg\_k(:,1), cs\%diag)}
\DoxyCodeLine{923     \textcolor{keywordflow}{if} (cs\%id\_ERb>0) \textcolor{keyword}{call }post\_data(cs\%id\_ERb, pe\_chg\_k(:,2), cs\%diag)}
\DoxyCodeLine{924     \textcolor{keywordflow}{if} (cs\%id\_ERc>0) \textcolor{keyword}{call }post\_data(cs\%id\_ERc, pe\_chg\_k(:,3), cs\%diag)}
\DoxyCodeLine{925     \textcolor{keywordflow}{if} (cs\%id\_ERh>0) \textcolor{keyword}{call }post\_data(cs\%id\_ERh, pe\_chg\_k(:,4), cs\%diag)}
\DoxyCodeLine{926     \textcolor{keywordflow}{if} (cs\%id\_Kddt>0) \textcolor{keyword}{call }post\_data(cs\%id\_Kddt, kddt\_h, cs\%diag)}
\DoxyCodeLine{927     \textcolor{keywordflow}{if} (cs\%id\_Kd>0)   \textcolor{keyword}{call }post\_data(cs\%id\_Kd,   kd, cs\%diag)}
\DoxyCodeLine{928     \textcolor{keywordflow}{if} (cs\%id\_h>0)    \textcolor{keyword}{call }post\_data(cs\%id\_h,    h\_tr, cs\%diag)}
\DoxyCodeLine{929     \textcolor{keywordflow}{if} (cs\%id\_zInt>0) \textcolor{keyword}{call }post\_data(cs\%id\_zInt, z\_int, cs\%diag)}
\DoxyCodeLine{930     \textcolor{keywordflow}{if} (cs\%id\_CHCt>0) \textcolor{keyword}{call }post\_data(cs\%id\_CHCt, colht\_cor\_k(:,1), cs\%diag)}
\DoxyCodeLine{931     \textcolor{keywordflow}{if} (cs\%id\_CHCb>0) \textcolor{keyword}{call }post\_data(cs\%id\_CHCb, colht\_cor\_k(:,2), cs\%diag)}
\DoxyCodeLine{932     \textcolor{keywordflow}{if} (cs\%id\_CHCc>0) \textcolor{keyword}{call }post\_data(cs\%id\_CHCc, colht\_cor\_k(:,3), cs\%diag)}
\DoxyCodeLine{933     \textcolor{keywordflow}{if} (cs\%id\_CHCh>0) \textcolor{keyword}{call }post\_data(cs\%id\_CHCh, colht\_cor\_k(:,4), cs\%diag)}
\DoxyCodeLine{934     \textcolor{keywordflow}{if} (cs\%id\_T0>0) \textcolor{keyword}{call }post\_data(cs\%id\_T0, t0, cs\%diag)}
\DoxyCodeLine{935     \textcolor{keywordflow}{if} (cs\%id\_Tf>0) \textcolor{keyword}{call }post\_data(cs\%id\_Tf, tf, cs\%diag)}
\DoxyCodeLine{936     \textcolor{keywordflow}{if} (cs\%id\_S0>0) \textcolor{keyword}{call }post\_data(cs\%id\_S0, s0, cs\%diag)}
\DoxyCodeLine{937     \textcolor{keywordflow}{if} (cs\%id\_Sf>0) \textcolor{keyword}{call }post\_data(cs\%id\_Sf, sf, cs\%diag)}
\DoxyCodeLine{938     \textcolor{keywordflow}{if} (cs\%id\_N2\_0>0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{939       n2(1) = 0.0 ; n2(nz+1) = 0.0}
\DoxyCodeLine{940       \textcolor{keywordflow}{do} k=2,nz}
\DoxyCodeLine{941         \textcolor{keyword}{call }calculate\_density(0.5*(t0(k-\/1) + t0(k)), 0.5*(s0(k-\/1) + s0(k)), \&}
\DoxyCodeLine{942                                pres(k), rho\_here, tv\%eqn\_of\_state)}
\DoxyCodeLine{943         n2(k) = ((us\%L\_to\_Z**2*gv\%g\_Earth) * rho\_here / (0.5*gv\%H\_to\_Z*(h\_tr(k-\/1) + h\_tr(k)))) * \&}
\DoxyCodeLine{944                 ( 0.5*(dsv\_dt(k-\/1) + dsv\_dt(k)) * (t0(k-\/1) -\/ t0(k)) + \&}
\DoxyCodeLine{945                   0.5*(dsv\_ds(k-\/1) + dsv\_ds(k)) * (s0(k-\/1) -\/ s0(k)) )}
\DoxyCodeLine{946 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{947       \textcolor{keyword}{call }post\_data(cs\%id\_N2\_0, n2, cs\%diag)}
\DoxyCodeLine{948 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{949     \textcolor{keywordflow}{if} (cs\%id\_N2\_f>0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{950       n2(1) = 0.0 ; n2(nz+1) = 0.0}
\DoxyCodeLine{951       \textcolor{keywordflow}{do} k=2,nz}
\DoxyCodeLine{952         \textcolor{keyword}{call }calculate\_density(0.5*(tf(k-\/1) + tf(k)), 0.5*(sf(k-\/1) + sf(k)), \&}
\DoxyCodeLine{953                                pres(k), rho\_here, tv\%eqn\_of\_state)}
\DoxyCodeLine{954         n2(k) = ((us\%L\_to\_Z**2*gv\%g\_Earth) * rho\_here / (0.5*gv\%H\_to\_Z*(h\_tr(k-\/1) + h\_tr(k)))) * \&}
\DoxyCodeLine{955                 ( 0.5*(dsv\_dt(k-\/1) + dsv\_dt(k)) * (tf(k-\/1) -\/ tf(k)) + \&}
\DoxyCodeLine{956                   0.5*(dsv\_ds(k-\/1) + dsv\_ds(k)) * (sf(k-\/1) -\/ sf(k)) )}
\DoxyCodeLine{957 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{958       \textcolor{keyword}{call }post\_data(cs\%id\_N2\_f, n2, cs\%diag)}
\DoxyCodeLine{959 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{960 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{961 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__diapyc__energy__req_aa4cb9f1b90a245e34ddd4182943a21c6}\label{namespacemom__diapyc__energy__req_aa4cb9f1b90a245e34ddd4182943a21c6}} 
\index{mom\_diapyc\_energy\_req@{mom\_diapyc\_energy\_req}!diapyc\_energy\_req\_end@{diapyc\_energy\_req\_end}}
\index{diapyc\_energy\_req\_end@{diapyc\_energy\_req\_end}!mom\_diapyc\_energy\_req@{mom\_diapyc\_energy\_req}}
\doxysubsubsection{\texorpdfstring{diapyc\_energy\_req\_end()}{diapyc\_energy\_req\_end()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+diapyc\+\_\+energy\+\_\+req\+::diapyc\+\_\+energy\+\_\+req\+\_\+end (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__diapyc__energy__req_1_1diapyc__energy__req__cs}{diapyc\+\_\+energy\+\_\+req\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



Clean up and deallocate memory associated with the diapycnal energy requirement module. 


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


Definition at line 1352 of file M\+O\+M\+\_\+diapyc\+\_\+energy\+\_\+req.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1352   \textcolor{keywordtype}{type}(diapyc\_energy\_req\_CS), \textcolor{keywordtype}{pointer} :: CS\textcolor{comment}{ !< Diapycnal energy requirement control structure that}}
\DoxyCodeLine{1353 \textcolor{comment}{                                            !! will be deallocated in this subroutine.}}
\DoxyCodeLine{1354   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keyword}{deallocate}(cs)}

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__diapyc__energy__req_a63b127bfd78461d8df3449591792b224}\label{namespacemom__diapyc__energy__req_a63b127bfd78461d8df3449591792b224}} 
\index{mom\_diapyc\_energy\_req@{mom\_diapyc\_energy\_req}!diapyc\_energy\_req\_init@{diapyc\_energy\_req\_init}}
\index{diapyc\_energy\_req\_init@{diapyc\_energy\_req\_init}!mom\_diapyc\_energy\_req@{mom\_diapyc\_energy\_req}}
\doxysubsubsection{\texorpdfstring{diapyc\_energy\_req\_init()}{diapyc\_energy\_req\_init()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+diapyc\+\_\+energy\+\_\+req\+::diapyc\+\_\+energy\+\_\+req\+\_\+init (\begin{DoxyParamCaption}\item[{type(time\+\_\+type), intent(in)}]{Time,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(param\+\_\+file\+\_\+type), intent(in)}]{param\+\_\+file,  }\item[{type(diag\+\_\+ctrl), intent(inout), target}]{diag,  }\item[{type(\mbox{\hyperlink{structmom__diapyc__energy__req_1_1diapyc__energy__req__cs}{diapyc\+\_\+energy\+\_\+req\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



Initialize parameters and allocate memory associated with the diapycnal energy requirement module. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em time} & model time \\
\hline
\mbox{\texttt{ in}}  & {\em g} & model grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & ocean vertical grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in}}  & {\em param\+\_\+file} & file to parse for parameter values \\
\hline
\mbox{\texttt{ in,out}}  & {\em diag} & structure to regulate diagnostic output \\
\hline
 & {\em cs} & module control structure \\
\hline
\end{DoxyParams}


Definition at line 1270 of file M\+O\+M\+\_\+diapyc\+\_\+energy\+\_\+req.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1270   \textcolor{keywordtype}{type}(time\_type),            \textcolor{keywordtype}{intent(in)}    :: Time\textcolor{comment}{        !< model time}}
\DoxyCodeLine{1271   \textcolor{keywordtype}{type}(ocean\_grid\_type),      \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{           !< model grid structure}}
\DoxyCodeLine{1272   \textcolor{keywordtype}{type}(verticalGrid\_type),    \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{          !< ocean vertical grid structure}}
\DoxyCodeLine{1273   \textcolor{keywordtype}{type}(unit\_scale\_type),      \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{          !< A dimensional unit scaling type}}
\DoxyCodeLine{1274   \textcolor{keywordtype}{type}(param\_file\_type),      \textcolor{keywordtype}{intent(in)}    :: param\_file\textcolor{comment}{  !< file to parse for parameter values}}
\DoxyCodeLine{1275   \textcolor{keywordtype}{type}(diag\_ctrl),    \textcolor{keywordtype}{target}, \textcolor{keywordtype}{intent(inout)} :: diag\textcolor{comment}{        !< structure to regulate diagnostic output}}
\DoxyCodeLine{1276   \textcolor{keywordtype}{type}(diapyc\_energy\_req\_CS), \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{          !< module control structure}}
\DoxyCodeLine{1277 }
\DoxyCodeLine{1278   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{save} :: init\_calls = 0}
\DoxyCodeLine{1279 \textcolor{comment}{! This include declares and sets the variable "version".}}
\DoxyCodeLine{1280 \textcolor{preprocessor}{\#include "version\_variable.h"}}
\DoxyCodeLine{1281 \textcolor{preprocessor}{}  \textcolor{keywordtype}{character(len=40)}  :: mdl = \textcolor{stringliteral}{"MOM\_diapyc\_energy\_req"} \textcolor{comment}{! This module's name.}}
\DoxyCodeLine{1282   \textcolor{keywordtype}{character(len=256)} :: mesg    \textcolor{comment}{! Message for error messages.}}
\DoxyCodeLine{1283 }
\DoxyCodeLine{1284   \textcolor{keywordflow}{if} (.not.\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{then} ; \textcolor{keyword}{allocate}(cs)}
\DoxyCodeLine{1285   \textcolor{keywordflow}{else} ; \textcolor{keywordflow}{return} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1286 }
\DoxyCodeLine{1287   cs\%initialized = .true.}
\DoxyCodeLine{1288   cs\%diag => diag}
\DoxyCodeLine{1289 }
\DoxyCodeLine{1290   \textcolor{comment}{! Read all relevant parameters and write them to the model log.}}
\DoxyCodeLine{1291   \textcolor{keyword}{call }log\_version(param\_file, mdl, version, \textcolor{stringliteral}{""})}
\DoxyCodeLine{1292   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ENERGY\_REQ\_KH\_SCALING"}, cs\%test\_Kh\_scaling, \&}
\DoxyCodeLine{1293                  \textcolor{stringliteral}{"A scaling factor for the diapycnal diffusivity used in "}//\&}
\DoxyCodeLine{1294                  \textcolor{stringliteral}{"testing the energy requirements."}, default=1.0, units=\textcolor{stringliteral}{"nondim"})}
\DoxyCodeLine{1295   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ENERGY\_REQ\_COL\_HT\_SCALING"}, cs\%ColHt\_scaling, \&}
\DoxyCodeLine{1296                  \textcolor{stringliteral}{"A scaling factor for the column height change correction "}//\&}
\DoxyCodeLine{1297                  \textcolor{stringliteral}{"used in testing the energy requirements."}, default=1.0, units=\textcolor{stringliteral}{"nondim"})}
\DoxyCodeLine{1298   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ENERGY\_REQ\_USE\_TEST\_PROFILE"}, \&}
\DoxyCodeLine{1299                  cs\%use\_test\_Kh\_profile, \&}
\DoxyCodeLine{1300                  \textcolor{stringliteral}{"If true, use the internal test diffusivity profile in "}//\&}
\DoxyCodeLine{1301                  \textcolor{stringliteral}{"place of any that might be passed in as an argument."}, default=.false.)}
\DoxyCodeLine{1302 }
\DoxyCodeLine{1303   cs\%id\_ERt = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_ERt'}, diag\%axesZi, time, \&}
\DoxyCodeLine{1304                  \textcolor{stringliteral}{"Diffusivity Energy Requirements, top-\/down"}, \&}
\DoxyCodeLine{1305                  \textcolor{stringliteral}{"J m-\/2"}, conversion=us\%RZ\_to\_kg\_m2*us\%L\_T\_to\_m\_s**2)}
\DoxyCodeLine{1306   cs\%id\_ERb = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_ERb'}, diag\%axesZi, time, \&}
\DoxyCodeLine{1307                  \textcolor{stringliteral}{"Diffusivity Energy Requirements, bottom-\/up"}, \&}
\DoxyCodeLine{1308                  \textcolor{stringliteral}{"J m-\/2"}, conversion=us\%RZ\_to\_kg\_m2*us\%L\_T\_to\_m\_s**2)}
\DoxyCodeLine{1309   cs\%id\_ERc = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_ERc'}, diag\%axesZi, time, \&}
\DoxyCodeLine{1310                  \textcolor{stringliteral}{"Diffusivity Energy Requirements, center-\/last"}, \&}
\DoxyCodeLine{1311                  \textcolor{stringliteral}{"J m-\/2"}, conversion=us\%RZ\_to\_kg\_m2*us\%L\_T\_to\_m\_s**2)}
\DoxyCodeLine{1312   cs\%id\_ERh = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_ERh'}, diag\%axesZi, time, \&}
\DoxyCodeLine{1313                  \textcolor{stringliteral}{"Diffusivity Energy Requirements, halves"}, \&}
\DoxyCodeLine{1314                  \textcolor{stringliteral}{"J m-\/2"}, conversion=us\%RZ\_to\_kg\_m2*us\%L\_T\_to\_m\_s**2)}
\DoxyCodeLine{1315   cs\%id\_Kddt = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_Kddt'}, diag\%axesZi, time, \&}
\DoxyCodeLine{1316                  \textcolor{stringliteral}{"Implicit diffusive coupling coefficient"}, \textcolor{stringliteral}{"m"}, conversion=gv\%H\_to\_m)}
\DoxyCodeLine{1317   cs\%id\_Kd = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_Kd'}, diag\%axesZi, time, \&}
\DoxyCodeLine{1318                  \textcolor{stringliteral}{"Diffusivity in test"}, \textcolor{stringliteral}{"m2 s-\/1"}, conversion=us\%Z\_to\_m**2)}
\DoxyCodeLine{1319   cs\%id\_h   = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_h\_lay'}, diag\%axesZL, time, \&}
\DoxyCodeLine{1320                  \textcolor{stringliteral}{"Test column layer thicknesses"}, \textcolor{stringliteral}{"m"}, conversion=gv\%H\_to\_m)}
\DoxyCodeLine{1321   cs\%id\_zInt = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_z\_int'}, diag\%axesZi, time, \&}
\DoxyCodeLine{1322                  \textcolor{stringliteral}{"Test column layer interface heights"}, \textcolor{stringliteral}{"m"}, conversion=gv\%H\_to\_m)}
\DoxyCodeLine{1323   cs\%id\_CHCt = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_CHCt'}, diag\%axesZi, time, \&}
\DoxyCodeLine{1324                  \textcolor{stringliteral}{"Column Height Correction to Energy Requirements, top-\/down"}, \&}
\DoxyCodeLine{1325                  \textcolor{stringliteral}{"J m-\/2"}, conversion=us\%RZ\_to\_kg\_m2*us\%L\_T\_to\_m\_s**2)}
\DoxyCodeLine{1326   cs\%id\_CHCb = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_CHCb'}, diag\%axesZi, time, \&}
\DoxyCodeLine{1327                  \textcolor{stringliteral}{"Column Height Correction to Energy Requirements, bottom-\/up"}, \&}
\DoxyCodeLine{1328                  \textcolor{stringliteral}{"J m-\/2"}, conversion=us\%RZ\_to\_kg\_m2*us\%L\_T\_to\_m\_s**2)}
\DoxyCodeLine{1329   cs\%id\_CHCc = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_CHCc'}, diag\%axesZi, time, \&}
\DoxyCodeLine{1330                  \textcolor{stringliteral}{"Column Height Correction to Energy Requirements, center-\/last"}, \&}
\DoxyCodeLine{1331                  \textcolor{stringliteral}{"J m-\/2"}, conversion=us\%RZ\_to\_kg\_m2*us\%L\_T\_to\_m\_s**2)}
\DoxyCodeLine{1332   cs\%id\_CHCh = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_CHCh'}, diag\%axesZi, time, \&}
\DoxyCodeLine{1333                  \textcolor{stringliteral}{"Column Height Correction to Energy Requirements, halves"}, \&}
\DoxyCodeLine{1334                  \textcolor{stringliteral}{"J m-\/2"}, conversion=us\%RZ\_to\_kg\_m2*us\%L\_T\_to\_m\_s**2)}
\DoxyCodeLine{1335   cs\%id\_T0 = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_T0'}, diag\%axesZL, time, \&}
\DoxyCodeLine{1336                  \textcolor{stringliteral}{"Temperature before mixing"}, \textcolor{stringliteral}{"deg C"})}
\DoxyCodeLine{1337   cs\%id\_Tf = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_Tf'}, diag\%axesZL, time, \&}
\DoxyCodeLine{1338                  \textcolor{stringliteral}{"Temperature after mixing"}, \textcolor{stringliteral}{"deg C"})}
\DoxyCodeLine{1339   cs\%id\_S0 = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_S0'}, diag\%axesZL, time, \&}
\DoxyCodeLine{1340                  \textcolor{stringliteral}{"Salinity before mixing"}, \textcolor{stringliteral}{"g kg-\/1"})}
\DoxyCodeLine{1341   cs\%id\_Sf = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_Sf'}, diag\%axesZL, time, \&}
\DoxyCodeLine{1342                  \textcolor{stringliteral}{"Salinity after mixing"}, \textcolor{stringliteral}{"g kg-\/1"})}
\DoxyCodeLine{1343   cs\%id\_N2\_0 = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_N2\_0'}, diag\%axesZi, time, \&}
\DoxyCodeLine{1344                  \textcolor{stringliteral}{"Squared buoyancy frequency before mixing"}, \textcolor{stringliteral}{"second-\/2"}, conversion=us\%s\_to\_T**2)}
\DoxyCodeLine{1345   cs\%id\_N2\_f = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_N2\_f'}, diag\%axesZi, time, \&}
\DoxyCodeLine{1346                  \textcolor{stringliteral}{"Squared buoyancy frequency after mixing"}, \textcolor{stringliteral}{"second-\/2"}, conversion=us\%s\_to\_T**2)}
\DoxyCodeLine{1347 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__diapyc__energy__req_a0bf0dd1f3ae4f7f66fb000322f18064e}\label{namespacemom__diapyc__energy__req_a0bf0dd1f3ae4f7f66fb000322f18064e}} 
\index{mom\_diapyc\_energy\_req@{mom\_diapyc\_energy\_req}!diapyc\_energy\_req\_test@{diapyc\_energy\_req\_test}}
\index{diapyc\_energy\_req\_test@{diapyc\_energy\_req\_test}!mom\_diapyc\_energy\_req@{mom\_diapyc\_energy\_req}}
\doxysubsubsection{\texorpdfstring{diapyc\_energy\_req\_test()}{diapyc\_energy\_req\_test()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+diapyc\+\_\+energy\+\_\+req\+::diapyc\+\_\+energy\+\_\+req\+\_\+test (\begin{DoxyParamCaption}\item[{real, dimension(g\%isd\+:g\%ied,g\%jsd\+:g\%jed,gv\%ke), intent(in)}]{h\+\_\+3d,  }\item[{real, intent(in)}]{dt,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(inout)}]{tv,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__diapyc__energy__req_1_1diapyc__energy__req__cs}{diapyc\+\_\+energy\+\_\+req\+\_\+cs}}), pointer}]{CS,  }\item[{real, dimension(g\%isd\+:g\%ied,g\%jsd\+:g\%jed,gv\%ke+1), intent(in), optional}]{Kd\+\_\+int }\end{DoxyParamCaption})}



This subroutine helps test the accuracy of the diapycnal mixing energy requirement code by writing diagnostics, possibly using an intensely mixing test profile of diffusivity. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & The ocean\textquotesingle{}s grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in}}  & {\em h\+\_\+3d} & Layer thickness before entrainment \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em tv} & A structure containing pointers to any available thermodynamic fields. Absent fields have N\+U\+LL ptrs. \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & The amount of time covered by this call \mbox{[}T $\sim$$>$ s\mbox{]}. \\
\hline
 & {\em cs} & This module\textquotesingle{}s control structure. \\
\hline
\mbox{\texttt{ in}}  & {\em kd\+\_\+int} & Interface diffusivities \mbox{[}Z2 T-\/1 $\sim$$>$ m2 s-\/1\mbox{]}. \\
\hline
\end{DoxyParams}


Definition at line 50 of file M\+O\+M\+\_\+diapyc\+\_\+energy\+\_\+req.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{50   \textcolor{keywordtype}{type}(ocean\_grid\_type),          \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{    !< The ocean's grid structure.}}
\DoxyCodeLine{51   \textcolor{keywordtype}{type}(verticalGrid\_type),        \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure.}}
\DoxyCodeLine{52   \textcolor{keywordtype}{type}(unit\_scale\_type),          \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{   !< A dimensional unit scaling type}}
\DoxyCodeLine{53 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(G\%isd:G\%ied,G\%jsd:G\%jed,GV\%ke)}, \&}
\DoxyCodeLine{54                                   \textcolor{keywordtype}{intent(in)}    :: h\_3d\textcolor{comment}{ !< Layer thickness before entrainment [H ~> m or kg m-\/2].}}
\DoxyCodeLine{55   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),          \textcolor{keywordtype}{intent(inout)} :: tv\textcolor{comment}{   !< A structure containing pointers to any}}
\DoxyCodeLine{56 \textcolor{comment}{                                                        !! available thermodynamic fields.}}
\DoxyCodeLine{57 \textcolor{comment}{                                                        !! Absent fields have NULL ptrs.}}
\DoxyCodeLine{58 \textcolor{keywordtype}{  real},                           \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{   !< The amount of time covered by this call [T ~> s].}}
\DoxyCodeLine{59   \textcolor{keywordtype}{type}(diapyc\_energy\_req\_CS),     \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{   !< This module's control structure.}}
\DoxyCodeLine{60 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(G\%isd:G\%ied,G\%jsd:G\%jed,GV\%ke+1)}, \&}
\DoxyCodeLine{61                         \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: Kd\_int\textcolor{comment}{ !< Interface diffusivities [Z2 T-\/1 ~> m2 s-\/1].}}
\DoxyCodeLine{62 }
\DoxyCodeLine{63   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{64 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(GV\%ke)} :: \&}
\DoxyCodeLine{65     T0, S0, \&   \textcolor{comment}{! T0 \& S0 are columns of initial temperatures and salinities [degC] and g/kg.}}
\DoxyCodeLine{66     h\_col       \textcolor{comment}{! h\_col is a column of thicknesses h at tracer points [H ~> m or kg m-\/2].}}
\DoxyCodeLine{67 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(GV\%ke+1)} :: \&}
\DoxyCodeLine{68     Kd, \&       \textcolor{comment}{! A column of diapycnal diffusivities at interfaces [Z2 T-\/1 ~> m2 s-\/1].}}
\DoxyCodeLine{69     h\_top, h\_bot \textcolor{comment}{! Distances from the top or bottom [H ~> m or kg m-\/2].}}
\DoxyCodeLine{70 \textcolor{keywordtype}{  real} :: ustar, absf, htot}
\DoxyCodeLine{71 \textcolor{keywordtype}{  real} :: energy\_Kd \textcolor{comment}{! The energy used by diapycnal mixing [W m-\/2].}}
\DoxyCodeLine{72 \textcolor{keywordtype}{  real} :: tmp1  \textcolor{comment}{! A temporary array.}}
\DoxyCodeLine{73   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, nz, itt}
\DoxyCodeLine{74   \textcolor{keywordtype}{logical} :: may\_print}
\DoxyCodeLine{75   is = g\%isc ; ie = g\%iec ; js = g\%jsc ; je = g\%jec ; nz = g\%ke}
\DoxyCodeLine{76 }
\DoxyCodeLine{77   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(cs)) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"diapyc\_energy\_req\_test: "}// \&}
\DoxyCodeLine{78          \textcolor{stringliteral}{"Module must be initialized before it is used."})}
\DoxyCodeLine{79 }
\DoxyCodeLine{80 \textcolor{comment}{!\$OMP do}}
\DoxyCodeLine{81   \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (g\%mask2dT(i,j) > 0.5) \textcolor{keywordflow}{then}}
\DoxyCodeLine{82     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_int) .and. .not.cs\%use\_test\_Kh\_profile) \textcolor{keywordflow}{then}}
\DoxyCodeLine{83       \textcolor{keywordflow}{do} k=1,nz+1 ; kd(k) = cs\%test\_Kh\_scaling*kd\_int(i,j,k) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{84     \textcolor{keywordflow}{else}}
\DoxyCodeLine{85       htot = 0.0 ; h\_top(1) = 0.0}
\DoxyCodeLine{86       \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{87         t0(k) = tv\%T(i,j,k) ; s0(k) = tv\%S(i,j,k)}
\DoxyCodeLine{88         h\_col(k) = h\_3d(i,j,k)}
\DoxyCodeLine{89         h\_top(k+1) = h\_top(k) + h\_col(k)}
\DoxyCodeLine{90 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{91       htot = h\_top(nz+1)}
\DoxyCodeLine{92       h\_bot(nz+1) = 0.0}
\DoxyCodeLine{93       \textcolor{keywordflow}{do} k=nz,1,-\/1}
\DoxyCodeLine{94         h\_bot(k) = h\_bot(k+1) + h\_col(k)}
\DoxyCodeLine{95 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{96 }
\DoxyCodeLine{97       ustar = 0.01*us\%m\_to\_Z*us\%T\_to\_s \textcolor{comment}{! Change this to being an input parameter?}}
\DoxyCodeLine{98       absf = 0.25*((abs(g\%CoriolisBu(i-\/1,j-\/1)) + abs(g\%CoriolisBu(i,j))) + \&}
\DoxyCodeLine{99                              (abs(g\%CoriolisBu(i-\/1,j-\/1)) + abs(g\%CoriolisBu(i,j))))}
\DoxyCodeLine{100       kd(1) = 0.0 ; kd(nz+1) = 0.0}
\DoxyCodeLine{101       \textcolor{keywordflow}{do} k=2,nz}
\DoxyCodeLine{102         tmp1 = h\_top(k) * h\_bot(k) * gv\%H\_to\_Z}
\DoxyCodeLine{103         kd(k) = cs\%test\_Kh\_scaling *  \&}
\DoxyCodeLine{104                 ustar * 0.41 * (tmp1*ustar) / (absf*tmp1 + htot*ustar)}
\DoxyCodeLine{105 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{106 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{107     may\_print = is\_root\_pe() .and. (i==ie) .and. (j==je)}
\DoxyCodeLine{108     \textcolor{keyword}{call }diapyc\_energy\_req\_calc(h\_col, t0, s0, kd, energy\_kd, dt, tv, g, gv, us, \&}
\DoxyCodeLine{109                                 may\_print=may\_print, cs=cs)}
\DoxyCodeLine{110 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{111 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__diapyc__energy__req_a2bed511a4b1df9de0e2230c24389bc82}\label{namespacemom__diapyc__energy__req_a2bed511a4b1df9de0e2230c24389bc82}} 
\index{mom\_diapyc\_energy\_req@{mom\_diapyc\_energy\_req}!find\_pe\_chg@{find\_pe\_chg}}
\index{find\_pe\_chg@{find\_pe\_chg}!mom\_diapyc\_energy\_req@{mom\_diapyc\_energy\_req}}
\doxysubsubsection{\texorpdfstring{find\_pe\_chg()}{find\_pe\_chg()}}
{\footnotesize\ttfamily subroutine mom\+\_\+diapyc\+\_\+energy\+\_\+req\+::find\+\_\+pe\+\_\+chg (\begin{DoxyParamCaption}\item[{real, intent(in)}]{Kddt\+\_\+h0,  }\item[{real, intent(in)}]{d\+Kddt\+\_\+h,  }\item[{real, intent(in)}]{hp\+\_\+a,  }\item[{real, intent(in)}]{hp\+\_\+b,  }\item[{real, intent(in)}]{Th\+\_\+a,  }\item[{real, intent(in)}]{Sh\+\_\+a,  }\item[{real, intent(in)}]{Th\+\_\+b,  }\item[{real, intent(in)}]{Sh\+\_\+b,  }\item[{real, intent(in)}]{d\+T\+\_\+to\+\_\+d\+P\+E\+\_\+a,  }\item[{real, intent(in)}]{d\+S\+\_\+to\+\_\+d\+P\+E\+\_\+a,  }\item[{real, intent(in)}]{d\+T\+\_\+to\+\_\+d\+P\+E\+\_\+b,  }\item[{real, intent(in)}]{d\+S\+\_\+to\+\_\+d\+P\+E\+\_\+b,  }\item[{real, intent(in)}]{pres\+\_\+Z,  }\item[{real, intent(in)}]{d\+T\+\_\+to\+\_\+d\+Col\+Ht\+\_\+a,  }\item[{real, intent(in)}]{d\+S\+\_\+to\+\_\+d\+Col\+Ht\+\_\+a,  }\item[{real, intent(in)}]{d\+T\+\_\+to\+\_\+d\+Col\+Ht\+\_\+b,  }\item[{real, intent(in)}]{d\+S\+\_\+to\+\_\+d\+Col\+Ht\+\_\+b,  }\item[{real, intent(out), optional}]{P\+E\+\_\+chg,  }\item[{real, intent(out), optional}]{d\+P\+Ec\+\_\+d\+Kd,  }\item[{real, intent(out), optional}]{d\+P\+E\+\_\+max,  }\item[{real, intent(out), optional}]{d\+P\+Ec\+\_\+d\+Kd\+\_\+0,  }\item[{real, intent(out), optional}]{Col\+Ht\+\_\+cor }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine calculates the change in potential energy and or derivatives for several changes in an interfaces\textquotesingle{}s diapycnal diffusivity times a timestep. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em kddt\+\_\+h0} & The previously used diffusivity at an interface times the time step and divided by the average of the thicknesses around the interface \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dkddt\+\_\+h} & The trial change in the diffusivity at an interface times the time step and divided by the average of the thicknesses around the interface \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em hp\+\_\+a} & The effective pivot thickness of the layer above the interface, given by h\+\_\+k plus a term that is a fraction (determined from the tridiagonal solver) of Kddt\+\_\+h for the interface above \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em hp\+\_\+b} & The effective pivot thickness of the layer below the interface, given by h\+\_\+k plus a term that is a fraction (determined from the tridiagonal solver) of Kddt\+\_\+h for the interface above \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em th\+\_\+a} & An effective temperature times a thickness in the layer above, including implicit mixing effects with other yet higher layers \mbox{[}degC H $\sim$$>$ degC m or degC kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em sh\+\_\+a} & An effective salinity times a thickness in the layer above, including implicit mixing effects with other yet higher layers \mbox{[}ppt H $\sim$$>$ ppt m or ppt kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em th\+\_\+b} & An effective temperature times a thickness in the layer below, including implicit mixing effects with other yet lower layers \mbox{[}degC H $\sim$$>$ degC m or degC kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em sh\+\_\+b} & An effective salinity times a thickness in the layer below, including implicit mixing effects with other yet lower layers \mbox{[}ppt H $\sim$$>$ ppt m or ppt kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dt\+\_\+to\+\_\+dpe\+\_\+a} & A factor (pres\+\_\+lay$\ast$mass\+\_\+lay$\ast$d\+Spec\+\_\+vol/dT) relating a layer\textquotesingle{}s temperature change to the change in column potential energy, including all implicit diffusive changes in the temperatures of all the layers above \mbox{[}R Z L2 T-\/2 deg\+C-\/1 $\sim$$>$ J m-\/2 deg\+C-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em ds\+\_\+to\+\_\+dpe\+\_\+a} & A factor (pres\+\_\+lay$\ast$mass\+\_\+lay$\ast$d\+Spec\+\_\+vol/dS) relating a layer\textquotesingle{}s salinity change to the change in column potential energy, including all implicit diffusive changes in the salinities of all the layers above \mbox{[}R Z L2 T-\/2 ppt-\/1 $\sim$$>$ J m-\/2 ppt-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dt\+\_\+to\+\_\+dpe\+\_\+b} & A factor (pres\+\_\+lay$\ast$mass\+\_\+lay$\ast$d\+Spec\+\_\+vol/dT) relating a layer\textquotesingle{}s temperature change to the change in column potential energy, including all implicit diffusive changes in the temperatures of all the layers below \mbox{[}R Z L2 T-\/2 deg\+C-\/1 $\sim$$>$ J m-\/2 deg\+C-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em ds\+\_\+to\+\_\+dpe\+\_\+b} & A factor (pres\+\_\+lay$\ast$mass\+\_\+lay$\ast$d\+Spec\+\_\+vol/dS) relating a layer\textquotesingle{}s salinity change to the change in column potential energy, including all implicit diffusive changes in the salinities of all the layers below \mbox{[}R Z L2 T-\/2 ppt-\/1 $\sim$$>$ J m-\/2 ppt-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em pres\+\_\+z} & The hydrostatic interface pressure, which is used to relate the changes in column thickness to the energy that is radiated as gravity waves and unavailable to drive mixing \mbox{[}R L2 T-\/2 m Z-\/1 $\sim$$>$ J m-\/3\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dt\+\_\+to\+\_\+dcolht\+\_\+a} & A factor (mass\+\_\+lay$\ast$d\+S\+Col\+Htc\+\_\+vol/dT) relating a layer\textquotesingle{}s temperature change to the change in column height, including all implicit diffusive changes in the temperatures of all the layers above \mbox{[}Z deg\+C-\/1 $\sim$$>$ m deg\+C-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em ds\+\_\+to\+\_\+dcolht\+\_\+a} & A factor (mass\+\_\+lay$\ast$d\+S\+Col\+Htc\+\_\+vol/dS) relating a layer\textquotesingle{}s salinity change to the change in column height, including all implicit diffusive changes in the salinities of all the layers above \mbox{[}Z ppt-\/1 $\sim$$>$ m ppt-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dt\+\_\+to\+\_\+dcolht\+\_\+b} & A factor (mass\+\_\+lay$\ast$d\+S\+Col\+Htc\+\_\+vol/dT) relating a layer\textquotesingle{}s temperature change to the change in column height, including all implicit diffusive changes in the temperatures of all the layers below \mbox{[}Z deg\+C-\/1 $\sim$$>$ m deg\+C-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em ds\+\_\+to\+\_\+dcolht\+\_\+b} & A factor (mass\+\_\+lay$\ast$d\+S\+Col\+Htc\+\_\+vol/dS) relating a layer\textquotesingle{}s salinity change to the change in column height, including all implicit diffusive changes in the salinities of all the layers below \mbox{[}Z ppt-\/1 $\sim$$>$ m ppt-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em pe\+\_\+chg} & The change in column potential energy from applying Kddt\+\_\+h at the present interface \mbox{[}R Z L2 T-\/2 $\sim$$>$ J m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em dpec\+\_\+dkd} & The partial derivative of P\+E\+\_\+chg with Kddt\+\_\+h, \mbox{[}R Z L2 T-\/2 H-\/1 $\sim$$>$ J m-\/3 or J kg-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em dpe\+\_\+max} & The maximum change in column potential energy that could be realizedd by applying a huge value of Kddt\+\_\+h at the present interface \mbox{[}R Z L2 T-\/2 $\sim$$>$ J m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em dpec\+\_\+dkd\+\_\+0} & The partial derivative of P\+E\+\_\+chg with Kddt\+\_\+h in the limit where Kddt\+\_\+h = 0 \mbox{[}R Z L2 T-\/2 H-\/1 $\sim$$>$ J m-\/3 or J kg-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em colht\+\_\+cor} & The correction to P\+E\+\_\+chg that is made due to a net change in the column height \mbox{[}R Z L2 T-\/2 $\sim$$>$ J m-\/2\mbox{]}. \\
\hline
\end{DoxyParams}


Definition at line 970 of file M\+O\+M\+\_\+diapyc\+\_\+energy\+\_\+req.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{970 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: Kddt\_h0\textcolor{comment}{  !< The previously used diffusivity at an interface times}}
\DoxyCodeLine{971 \textcolor{comment}{                                !! the time step and  divided by the average of the}}
\DoxyCodeLine{972 \textcolor{comment}{                                !! thicknesses around the interface [H ~> m or kg m-\/2].}}
\DoxyCodeLine{973 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dKddt\_h\textcolor{comment}{  !< The trial change in the diffusivity at an interface times}}
\DoxyCodeLine{974 \textcolor{comment}{                                !! the time step and  divided by the average of the}}
\DoxyCodeLine{975 \textcolor{comment}{                                !! thicknesses around the interface [H ~> m or kg m-\/2].}}
\DoxyCodeLine{976 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: hp\_a\textcolor{comment}{     !< The effective pivot thickness of the layer above the}}
\DoxyCodeLine{977 \textcolor{comment}{                                !! interface, given by h\_k plus a term that}}
\DoxyCodeLine{978 \textcolor{comment}{                                !! is a fraction (determined from the tridiagonal solver) of}}
\DoxyCodeLine{979 \textcolor{comment}{                                !! Kddt\_h for the interface above [H ~> m or kg m-\/2].}}
\DoxyCodeLine{980 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: hp\_b\textcolor{comment}{     !< The effective pivot thickness of the layer below the}}
\DoxyCodeLine{981 \textcolor{comment}{                                !! interface, given by h\_k plus a term that}}
\DoxyCodeLine{982 \textcolor{comment}{                                !! is a fraction (determined from the tridiagonal solver) of}}
\DoxyCodeLine{983 \textcolor{comment}{                                !! Kddt\_h for the interface above [H ~> m or kg m-\/2].}}
\DoxyCodeLine{984 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: Th\_a\textcolor{comment}{     !< An effective temperature times a thickness in the layer}}
\DoxyCodeLine{985 \textcolor{comment}{                                !! above, including implicit mixing effects with other}}
\DoxyCodeLine{986 \textcolor{comment}{                                !! yet higher layers [degC H ~> degC m or degC kg m-\/2].}}
\DoxyCodeLine{987 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: Sh\_a\textcolor{comment}{     !< An effective salinity times a thickness in the layer}}
\DoxyCodeLine{988 \textcolor{comment}{                                !! above, including implicit mixing effects with other}}
\DoxyCodeLine{989 \textcolor{comment}{                                !! yet higher layers [ppt H ~> ppt m or ppt kg m-\/2].}}
\DoxyCodeLine{990 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: Th\_b\textcolor{comment}{     !< An effective temperature times a thickness in the layer}}
\DoxyCodeLine{991 \textcolor{comment}{                                !! below, including implicit mixing effects with other}}
\DoxyCodeLine{992 \textcolor{comment}{                                !! yet lower layers [degC H ~> degC m or degC kg m-\/2].}}
\DoxyCodeLine{993 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: Sh\_b\textcolor{comment}{     !< An effective salinity times a thickness in the layer}}
\DoxyCodeLine{994 \textcolor{comment}{                                !! below, including implicit mixing effects with other}}
\DoxyCodeLine{995 \textcolor{comment}{                                !! yet lower layers [ppt H ~> ppt m or ppt kg m-\/2].}}
\DoxyCodeLine{996 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dT\_to\_dPE\_a\textcolor{comment}{ !< A factor (pres\_lay*mass\_lay*dSpec\_vol/dT) relating}}
\DoxyCodeLine{997 \textcolor{comment}{                                !! a layer's temperature change to the change in column}}
\DoxyCodeLine{998 \textcolor{comment}{                                !! potential energy, including all implicit diffusive changes}}
\DoxyCodeLine{999 \textcolor{comment}{                                !! in the temperatures of all the layers above [R Z L2 T-\/2 degC-\/1 ~> J m-\/2 degC-\/1].}}
\DoxyCodeLine{1000 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dS\_to\_dPE\_a\textcolor{comment}{ !< A factor (pres\_lay*mass\_lay*dSpec\_vol/dS) relating}}
\DoxyCodeLine{1001 \textcolor{comment}{                                !! a layer's salinity change to the change in column}}
\DoxyCodeLine{1002 \textcolor{comment}{                                !! potential energy, including all implicit diffusive changes}}
\DoxyCodeLine{1003 \textcolor{comment}{                                !! in the salinities of all the layers above [R Z L2 T-\/2 ppt-\/1 ~> J m-\/2 ppt-\/1].}}
\DoxyCodeLine{1004 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dT\_to\_dPE\_b\textcolor{comment}{ !< A factor (pres\_lay*mass\_lay*dSpec\_vol/dT) relating}}
\DoxyCodeLine{1005 \textcolor{comment}{                                !! a layer's temperature change to the change in column}}
\DoxyCodeLine{1006 \textcolor{comment}{                                !! potential energy, including all implicit diffusive changes}}
\DoxyCodeLine{1007 \textcolor{comment}{                                !! in the temperatures of all the layers below [R Z L2 T-\/2 degC-\/1 ~> J m-\/2 degC-\/1].}}
\DoxyCodeLine{1008 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dS\_to\_dPE\_b\textcolor{comment}{ !< A factor (pres\_lay*mass\_lay*dSpec\_vol/dS) relating}}
\DoxyCodeLine{1009 \textcolor{comment}{                                !! a layer's salinity change to the change in column}}
\DoxyCodeLine{1010 \textcolor{comment}{                                !! potential energy, including all implicit diffusive changes}}
\DoxyCodeLine{1011 \textcolor{comment}{                                !! in the salinities of all the layers below [R Z L2 T-\/2 ppt-\/1 ~> J m-\/2 ppt-\/1].}}
\DoxyCodeLine{1012 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: pres\_Z\textcolor{comment}{   !< The hydrostatic interface pressure, which is used to relate}}
\DoxyCodeLine{1013 \textcolor{comment}{                                !! the changes in column thickness to the energy that is radiated}}
\DoxyCodeLine{1014 \textcolor{comment}{                                !! as gravity waves and unavailable to drive mixing [R L2 T-\/2 m Z-\/1 ~> J m-\/3].}}
\DoxyCodeLine{1015 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dT\_to\_dColHt\_a\textcolor{comment}{ !< A factor (mass\_lay*dSColHtc\_vol/dT) relating}}
\DoxyCodeLine{1016 \textcolor{comment}{                                !! a layer's temperature change to the change in column}}
\DoxyCodeLine{1017 \textcolor{comment}{                                !! height, including all implicit diffusive changes}}
\DoxyCodeLine{1018 \textcolor{comment}{                                !! in the temperatures of all the layers above [Z degC-\/1 ~> m degC-\/1].}}
\DoxyCodeLine{1019 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dS\_to\_dColHt\_a\textcolor{comment}{ !< A factor (mass\_lay*dSColHtc\_vol/dS) relating}}
\DoxyCodeLine{1020 \textcolor{comment}{                                !! a layer's salinity change to the change in column}}
\DoxyCodeLine{1021 \textcolor{comment}{                                !! height, including all implicit diffusive changes}}
\DoxyCodeLine{1022 \textcolor{comment}{                                !! in the salinities of all the layers above [Z ppt-\/1 ~> m ppt-\/1].}}
\DoxyCodeLine{1023 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dT\_to\_dColHt\_b\textcolor{comment}{ !< A factor (mass\_lay*dSColHtc\_vol/dT) relating}}
\DoxyCodeLine{1024 \textcolor{comment}{                                !! a layer's temperature change to the change in column}}
\DoxyCodeLine{1025 \textcolor{comment}{                                !! height, including all implicit diffusive changes}}
\DoxyCodeLine{1026 \textcolor{comment}{                                !! in the temperatures of all the layers below [Z degC-\/1 ~> m degC-\/1].}}
\DoxyCodeLine{1027 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dS\_to\_dColHt\_b\textcolor{comment}{ !< A factor (mass\_lay*dSColHtc\_vol/dS) relating}}
\DoxyCodeLine{1028 \textcolor{comment}{                                !! a layer's salinity change to the change in column}}
\DoxyCodeLine{1029 \textcolor{comment}{                                !! height, including all implicit diffusive changes}}
\DoxyCodeLine{1030 \textcolor{comment}{                                !! in the salinities of all the layers below [Z ppt-\/1 ~> m ppt-\/1].}}
\DoxyCodeLine{1031 }
\DoxyCodeLine{1032 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: PE\_chg\textcolor{comment}{   !< The change in column potential energy from applying}}
\DoxyCodeLine{1033 \textcolor{comment}{                                          !! Kddt\_h at the present interface [R Z L2 T-\/2 ~> J m-\/2].}}
\DoxyCodeLine{1034 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: dPEc\_dKd\textcolor{comment}{ !< The partial derivative of PE\_chg with Kddt\_h,}}
\DoxyCodeLine{1035 \textcolor{comment}{                                          !! [R Z L2 T-\/2 H-\/1 ~> J m-\/3 or J kg-\/1].}}
\DoxyCodeLine{1036 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: dPE\_max\textcolor{comment}{  !< The maximum change in column potential energy that could}}
\DoxyCodeLine{1037 \textcolor{comment}{                                          !! be realizedd by applying a huge value of Kddt\_h at the}}
\DoxyCodeLine{1038 \textcolor{comment}{                                          !! present interface [R Z L2 T-\/2 ~> J m-\/2].}}
\DoxyCodeLine{1039 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: dPEc\_dKd\_0\textcolor{comment}{ !< The partial derivative of PE\_chg with Kddt\_h in the}}
\DoxyCodeLine{1040 \textcolor{comment}{                                            !! limit where Kddt\_h = 0 [R Z L2 T-\/2 H-\/1 ~> J m-\/3 or J kg-\/1].}}
\DoxyCodeLine{1041 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: ColHt\_cor\textcolor{comment}{  !< The correction to PE\_chg that is made due to a net}}
\DoxyCodeLine{1042 \textcolor{comment}{                                            !! change in the column height [R Z L2 T-\/2 ~> J m-\/2].}}
\DoxyCodeLine{1043 }
\DoxyCodeLine{1044 \textcolor{keywordtype}{  real} :: hps  \textcolor{comment}{! The sum of the two effective pivot thicknesses [H ~> m or kg m-\/2].}}
\DoxyCodeLine{1045 \textcolor{keywordtype}{  real} :: bdt1 \textcolor{comment}{! A product of the two pivot thicknesses plus a diffusive term [H2 ~> m2 or kg2 m-\/4].}}
\DoxyCodeLine{1046 \textcolor{keywordtype}{  real} :: dT\_c \textcolor{comment}{! The core term in the expressions for the temperature changes [degC H2 ~> degC m2 or degC kg2 m-\/4].}}
\DoxyCodeLine{1047 \textcolor{keywordtype}{  real} :: dS\_c \textcolor{comment}{! The core term in the expressions for the salinity changes [psu H2 ~> psu m2 or psu kg2 m-\/4].}}
\DoxyCodeLine{1048 \textcolor{keywordtype}{  real} :: PEc\_core \textcolor{comment}{! The diffusivity-\/independent core term in the expressions}}
\DoxyCodeLine{1049                    \textcolor{comment}{! for the potential energy changes [R L2 T-\/2 ~> J m-\/3].}}
\DoxyCodeLine{1050 \textcolor{keywordtype}{  real} :: ColHt\_core \textcolor{comment}{! The diffusivity-\/independent core term in the expressions}}
\DoxyCodeLine{1051                      \textcolor{comment}{! for the column height changes [R L2 T-\/2 ~> J m-\/3].}}
\DoxyCodeLine{1052 \textcolor{keywordtype}{  real} :: ColHt\_chg  \textcolor{comment}{! The change in the column height [Z ~> m].}}
\DoxyCodeLine{1053 \textcolor{keywordtype}{  real} :: y1   \textcolor{comment}{! A local temporary term, in [H-\/3] or [H-\/4] in various contexts.}}
\DoxyCodeLine{1054 }
\DoxyCodeLine{1055   \textcolor{comment}{!   The expression for the change in potential energy used here is derived}}
\DoxyCodeLine{1056   \textcolor{comment}{! from the expression for the final estimates of the changes in temperature}}
\DoxyCodeLine{1057   \textcolor{comment}{! and salinities, and then extensively manipulated to get it into its most}}
\DoxyCodeLine{1058   \textcolor{comment}{! succint form. The derivation is not necessarily obvious, but it demonstrably}}
\DoxyCodeLine{1059   \textcolor{comment}{! works by comparison with separate calculations of the energy changes after}}
\DoxyCodeLine{1060   \textcolor{comment}{! the tridiagonal solver for the final changes in temperature and salinity are}}
\DoxyCodeLine{1061   \textcolor{comment}{! applied.}}
\DoxyCodeLine{1062 }
\DoxyCodeLine{1063   hps = hp\_a + hp\_b}
\DoxyCodeLine{1064   bdt1 = hp\_a * hp\_b + kddt\_h0 * hps}
\DoxyCodeLine{1065   dt\_c = hp\_a * th\_b -\/ hp\_b * th\_a}
\DoxyCodeLine{1066   ds\_c = hp\_a * sh\_b -\/ hp\_b * sh\_a}
\DoxyCodeLine{1067   pec\_core = hp\_b * (dt\_to\_dpe\_a * dt\_c + ds\_to\_dpe\_a * ds\_c) -\/ \&}
\DoxyCodeLine{1068              hp\_a * (dt\_to\_dpe\_b * dt\_c + ds\_to\_dpe\_b * ds\_c)}
\DoxyCodeLine{1069   colht\_core = hp\_b * (dt\_to\_dcolht\_a * dt\_c + ds\_to\_dcolht\_a * ds\_c) -\/ \&}
\DoxyCodeLine{1070                hp\_a * (dt\_to\_dcolht\_b * dt\_c + ds\_to\_dcolht\_b * ds\_c)}
\DoxyCodeLine{1071 }
\DoxyCodeLine{1072   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(pe\_chg)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1073     \textcolor{comment}{! Find the change in column potential energy due to the change in the}}
\DoxyCodeLine{1074     \textcolor{comment}{! diffusivity at this interface by dKddt\_h.}}
\DoxyCodeLine{1075     y1 = dkddt\_h / (bdt1 * (bdt1 + dkddt\_h * hps))}
\DoxyCodeLine{1076     pe\_chg = pec\_core * y1}
\DoxyCodeLine{1077     colht\_chg = colht\_core * y1}
\DoxyCodeLine{1078     \textcolor{keywordflow}{if} (colht\_chg < 0.0) pe\_chg = pe\_chg -\/ pres\_z * colht\_chg}
\DoxyCodeLine{1079     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(colht\_cor)) colht\_cor = -\/pres\_z * min(colht\_chg, 0.0)}
\DoxyCodeLine{1080   \textcolor{keywordflow}{elseif} (\textcolor{keyword}{present}(colht\_cor)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1081     y1 = dkddt\_h / (bdt1 * (bdt1 + dkddt\_h * hps))}
\DoxyCodeLine{1082     colht\_cor = -\/pres\_z * min(colht\_core * y1, 0.0)}
\DoxyCodeLine{1083 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1084 }
\DoxyCodeLine{1085   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dpec\_dkd)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1086     \textcolor{comment}{! Find the derivative of the potential energy change with dKddt\_h.}}
\DoxyCodeLine{1087     y1 = 1.0 / (bdt1 + dkddt\_h * hps)**2}
\DoxyCodeLine{1088     dpec\_dkd = pec\_core * y1}
\DoxyCodeLine{1089     colht\_chg = colht\_core * y1}
\DoxyCodeLine{1090     \textcolor{keywordflow}{if} (colht\_chg < 0.0) dpec\_dkd = dpec\_dkd -\/ pres\_z * colht\_chg}
\DoxyCodeLine{1091 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1092 }
\DoxyCodeLine{1093   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dpe\_max)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1094     \textcolor{comment}{! This expression is the limit of PE\_chg for infinite dKddt\_h.}}
\DoxyCodeLine{1095     y1 = 1.0 / (bdt1 * hps)}
\DoxyCodeLine{1096     dpe\_max = pec\_core * y1}
\DoxyCodeLine{1097     colht\_chg = colht\_core * y1}
\DoxyCodeLine{1098     \textcolor{keywordflow}{if} (colht\_chg < 0.0) dpe\_max = dpe\_max -\/ pres\_z * colht\_chg}
\DoxyCodeLine{1099 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1100 }
\DoxyCodeLine{1101   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dpec\_dkd\_0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1102     \textcolor{comment}{! This expression is the limit of dPEc\_dKd for dKddt\_h = 0.}}
\DoxyCodeLine{1103     y1 = 1.0 / bdt1**2}
\DoxyCodeLine{1104     dpec\_dkd\_0 = pec\_core * y1}
\DoxyCodeLine{1105     colht\_chg = colht\_core * y1}
\DoxyCodeLine{1106     \textcolor{keywordflow}{if} (colht\_chg < 0.0) dpec\_dkd\_0 = dpec\_dkd\_0 -\/ pres\_z * colht\_chg}
\DoxyCodeLine{1107 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1108 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__diapyc__energy__req_afb1736a09e8f04ae84f561924e157691}\label{namespacemom__diapyc__energy__req_afb1736a09e8f04ae84f561924e157691}} 
\index{mom\_diapyc\_energy\_req@{mom\_diapyc\_energy\_req}!find\_pe\_chg\_orig@{find\_pe\_chg\_orig}}
\index{find\_pe\_chg\_orig@{find\_pe\_chg\_orig}!mom\_diapyc\_energy\_req@{mom\_diapyc\_energy\_req}}
\doxysubsubsection{\texorpdfstring{find\_pe\_chg\_orig()}{find\_pe\_chg\_orig()}}
{\footnotesize\ttfamily subroutine mom\+\_\+diapyc\+\_\+energy\+\_\+req\+::find\+\_\+pe\+\_\+chg\+\_\+orig (\begin{DoxyParamCaption}\item[{real, intent(in)}]{Kddt\+\_\+h,  }\item[{real, intent(in)}]{h\+\_\+k,  }\item[{real, intent(in)}]{b\+\_\+den\+\_\+1,  }\item[{real, intent(in)}]{d\+Te\+\_\+term,  }\item[{real, intent(in)}]{d\+Se\+\_\+term,  }\item[{real, intent(in)}]{d\+T\+\_\+km1\+\_\+t2,  }\item[{real, intent(in)}]{d\+S\+\_\+km1\+\_\+t2,  }\item[{real, intent(in)}]{d\+T\+\_\+to\+\_\+d\+P\+E\+\_\+k,  }\item[{real, intent(in)}]{d\+S\+\_\+to\+\_\+d\+P\+E\+\_\+k,  }\item[{real, intent(in)}]{d\+T\+\_\+to\+\_\+d\+P\+Ea,  }\item[{real, intent(in)}]{d\+S\+\_\+to\+\_\+d\+P\+Ea,  }\item[{real, intent(in)}]{pres\+\_\+Z,  }\item[{real, intent(in)}]{d\+T\+\_\+to\+\_\+d\+Col\+Ht\+\_\+k,  }\item[{real, intent(in)}]{d\+S\+\_\+to\+\_\+d\+Col\+Ht\+\_\+k,  }\item[{real, intent(in)}]{d\+T\+\_\+to\+\_\+d\+Col\+Hta,  }\item[{real, intent(in)}]{d\+S\+\_\+to\+\_\+d\+Col\+Hta,  }\item[{real, intent(out), optional}]{P\+E\+\_\+chg,  }\item[{real, intent(out), optional}]{d\+P\+Ec\+\_\+d\+Kd,  }\item[{real, intent(out), optional}]{d\+P\+E\+\_\+max,  }\item[{real, intent(out), optional}]{d\+P\+Ec\+\_\+d\+Kd\+\_\+0 }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine calculates the change in potential energy and or derivatives for several changes in an interfaces\textquotesingle{}s diapycnal diffusivity times a timestep using the original form used in the first version of e\+P\+BL. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em kddt\+\_\+h} & The diffusivity at an interface times the time step and divided by the average of the thicknesses around the interface \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em h\+\_\+k} & The thickness of the layer below the interface \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em b\+\_\+den\+\_\+1} & The first term in the denominator of the pivot for the tridiagonal solver, given by h\+\_\+k plus a term that is a fraction (determined from the tridiagonal solver) of Kddt\+\_\+h for the interface above \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dte\+\_\+term} & A diffusivity-\/independent term related to the temperature change in the layer below the interface \mbox{[}degC H $\sim$$>$ degC m or degC kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dse\+\_\+term} & A diffusivity-\/independent term related to the salinity change in the layer below the interface \mbox{[}ppt H $\sim$$>$ ppt m or ppt kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dt\+\_\+km1\+\_\+t2} & A diffusivity-\/independent term related to the temperature change in the layer above the interface \mbox{[}degC\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em ds\+\_\+km1\+\_\+t2} & A diffusivity-\/independent term related to the salinity change in the layer above the interface \mbox{[}ppt\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em pres\+\_\+z} & The hydrostatic interface pressure, which is used to relate the changes in column thickness to the energy that is radiated as gravity waves and unavailable to drive mixing \mbox{[}R L2 T-\/2 $\sim$$>$ J m-\/3\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dt\+\_\+to\+\_\+dpe\+\_\+k} & A factor (pres\+\_\+lay$\ast$mass\+\_\+lay$\ast$d\+Spec\+\_\+vol/dT) relating a layer\textquotesingle{}s temperature change to the change in column potential energy, including all implicit diffusive changes in the temperatures of all the layers below \mbox{[}R Z L2 T-\/2 deg\+C-\/1 $\sim$$>$ J m-\/2 deg\+C-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em ds\+\_\+to\+\_\+dpe\+\_\+k} & A factor (pres\+\_\+lay$\ast$mass\+\_\+lay$\ast$d\+Spec\+\_\+vol/dS) relating a layer\textquotesingle{}s salinity change to the change in column potential energy, including all implicit diffusive changes in the salinities of all the layers below \mbox{[}R Z L2 T-\/2 ppt-\/1 $\sim$$>$ J m-\/2 ppt-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dt\+\_\+to\+\_\+dpea} & A factor (pres\+\_\+lay$\ast$mass\+\_\+lay$\ast$d\+Spec\+\_\+vol/dT) relating a layer\textquotesingle{}s temperature change to the change in column potential energy, including all implicit diffusive changes in the temperatures of all the layers above \mbox{[}R Z L2 T-\/2 deg\+C-\/1 $\sim$$>$ J m-\/2 deg\+C-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em ds\+\_\+to\+\_\+dpea} & A factor (pres\+\_\+lay$\ast$mass\+\_\+lay$\ast$d\+Spec\+\_\+vol/dS) relating a layer\textquotesingle{}s salinity change to the change in column potential energy, including all implicit diffusive changes in the salinities of all the layers above \mbox{[}R Z L2 T-\/2 ppt-\/1 $\sim$$>$ J m-\/2 ppt-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dt\+\_\+to\+\_\+dcolht\+\_\+k} & A factor (mass\+\_\+lay$\ast$d\+S\+Col\+Htc\+\_\+vol/dT) relating a layer\textquotesingle{}s temperature change to the change in column height, including all implicit diffusive changes in the temperatures of all the layers below \mbox{[}Z deg\+C-\/1 $\sim$$>$ m deg\+C-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em ds\+\_\+to\+\_\+dcolht\+\_\+k} & A factor (mass\+\_\+lay$\ast$d\+S\+Col\+Htc\+\_\+vol/dS) relating a layer\textquotesingle{}s salinity change to the change in column height, including all implicit diffusive changes in the salinities of all the layers below \mbox{[}Z ppt-\/1 $\sim$$>$ m ppt-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dt\+\_\+to\+\_\+dcolhta} & A factor (mass\+\_\+lay$\ast$d\+S\+Col\+Htc\+\_\+vol/dT) relating a layer\textquotesingle{}s temperature change to the change in column height, including all implicit diffusive changes in the temperatures of all the layers above \mbox{[}Z deg\+C-\/1 $\sim$$>$ m deg\+C-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em ds\+\_\+to\+\_\+dcolhta} & A factor (mass\+\_\+lay$\ast$d\+S\+Col\+Htc\+\_\+vol/dS) relating a layer\textquotesingle{}s salinity change to the change in column height, including all implicit diffusive changes in the salinities of all the layers above \mbox{[}Z ppt-\/1 $\sim$$>$ m ppt-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em pe\+\_\+chg} & The change in column potential energy from applying Kddt\+\_\+h at the present interface \mbox{[}R Z L2 T-\/2 $\sim$$>$ J m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em dpec\+\_\+dkd} & The partial derivative of P\+E\+\_\+chg with Kddt\+\_\+h, \mbox{[}R Z L2 T-\/2 H-\/1 $\sim$$>$ J m-\/3 or J kg-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em dpe\+\_\+max} & The maximum change in column potential energy that could be realized by applying a huge value of Kddt\+\_\+h at the present interface \mbox{[}R Z L2 T-\/2 $\sim$$>$ J m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em dpec\+\_\+dkd\+\_\+0} & The partial derivative of P\+E\+\_\+chg with Kddt\+\_\+h in the limit where Kddt\+\_\+h = 0 \mbox{[}R Z L2 T-\/2 H-\/1 $\sim$$>$ J m-\/3 or J kg-\/1\mbox{]}. \\
\hline
\end{DoxyParams}


Definition at line 1120 of file M\+O\+M\+\_\+diapyc\+\_\+energy\+\_\+req.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1120 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: Kddt\_h\textcolor{comment}{   !< The diffusivity at an interface times the time step and}}
\DoxyCodeLine{1121 \textcolor{comment}{                                !! divided by the average of the thicknesses around the}}
\DoxyCodeLine{1122 \textcolor{comment}{                                !! interface [H ~> m or kg m-\/2].}}
\DoxyCodeLine{1123 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: h\_k\textcolor{comment}{      !< The thickness of the layer below the interface [H ~> m or kg m-\/2].}}
\DoxyCodeLine{1124 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: b\_den\_1\textcolor{comment}{  !< The first term in the denominator of the pivot}}
\DoxyCodeLine{1125 \textcolor{comment}{                                !! for the tridiagonal solver, given by h\_k plus a term that}}
\DoxyCodeLine{1126 \textcolor{comment}{                                !! is a fraction (determined from the tridiagonal solver) of}}
\DoxyCodeLine{1127 \textcolor{comment}{                                !! Kddt\_h for the interface above [H ~> m or kg m-\/2].}}
\DoxyCodeLine{1128 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dTe\_term\textcolor{comment}{ !< A diffusivity-\/independent term related to the temperature change}}
\DoxyCodeLine{1129 \textcolor{comment}{                                !! in the layer below the interface [degC H ~> degC m or degC kg m-\/2].}}
\DoxyCodeLine{1130 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dSe\_term\textcolor{comment}{ !< A diffusivity-\/independent term related to the salinity change}}
\DoxyCodeLine{1131 \textcolor{comment}{                                !! in the layer below the interface [ppt H ~> ppt m or ppt kg m-\/2].}}
\DoxyCodeLine{1132 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dT\_km1\_t2\textcolor{comment}{ !< A diffusivity-\/independent term related to the}}
\DoxyCodeLine{1133 \textcolor{comment}{                                 !! temperature change in the layer above the interface [degC].}}
\DoxyCodeLine{1134 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dS\_km1\_t2\textcolor{comment}{ !< A diffusivity-\/independent term related to the}}
\DoxyCodeLine{1135 \textcolor{comment}{                                 !! salinity change in the layer above the interface [ppt].}}
\DoxyCodeLine{1136 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: pres\_Z\textcolor{comment}{    !< The hydrostatic interface pressure, which is used to relate}}
\DoxyCodeLine{1137 \textcolor{comment}{                                 !! the changes in column thickness to the energy that is radiated}}
\DoxyCodeLine{1138 \textcolor{comment}{                                 !! as gravity waves and unavailable to drive mixing [R L2 T-\/2 ~> J m-\/3].}}
\DoxyCodeLine{1139 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dT\_to\_dPE\_k\textcolor{comment}{ !< A factor (pres\_lay*mass\_lay*dSpec\_vol/dT) relating}}
\DoxyCodeLine{1140 \textcolor{comment}{                                 !! a layer's temperature change to the change in column}}
\DoxyCodeLine{1141 \textcolor{comment}{                                 !! potential energy, including all implicit diffusive changes}}
\DoxyCodeLine{1142 \textcolor{comment}{                                 !! in the temperatures of all the layers below [R Z L2 T-\/2 degC-\/1 ~> J m-\/2 degC-\/1].}}
\DoxyCodeLine{1143 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dS\_to\_dPE\_k\textcolor{comment}{ !< A factor (pres\_lay*mass\_lay*dSpec\_vol/dS) relating}}
\DoxyCodeLine{1144 \textcolor{comment}{                                 !! a layer's salinity change to the change in column}}
\DoxyCodeLine{1145 \textcolor{comment}{                                 !! potential energy, including all implicit diffusive changes}}
\DoxyCodeLine{1146 \textcolor{comment}{                                 !! in the salinities of all the layers below [R Z L2 T-\/2 ppt-\/1 ~> J m-\/2 ppt-\/1].}}
\DoxyCodeLine{1147 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dT\_to\_dPEa\textcolor{comment}{ !< A factor (pres\_lay*mass\_lay*dSpec\_vol/dT) relating}}
\DoxyCodeLine{1148 \textcolor{comment}{                                 !! a layer's temperature change to the change in column}}
\DoxyCodeLine{1149 \textcolor{comment}{                                 !! potential energy, including all implicit diffusive changes}}
\DoxyCodeLine{1150 \textcolor{comment}{                                 !! in the temperatures of all the layers above [R Z L2 T-\/2 degC-\/1 ~> J m-\/2 degC-\/1].}}
\DoxyCodeLine{1151 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dS\_to\_dPEa\textcolor{comment}{ !< A factor (pres\_lay*mass\_lay*dSpec\_vol/dS) relating}}
\DoxyCodeLine{1152 \textcolor{comment}{                                 !! a layer's salinity change to the change in column}}
\DoxyCodeLine{1153 \textcolor{comment}{                                 !! potential energy, including all implicit diffusive changes}}
\DoxyCodeLine{1154 \textcolor{comment}{                                 !! in the salinities of all the layers above [R Z L2 T-\/2 ppt-\/1 ~> J m-\/2 ppt-\/1].}}
\DoxyCodeLine{1155 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dT\_to\_dColHt\_k\textcolor{comment}{ !< A factor (mass\_lay*dSColHtc\_vol/dT) relating}}
\DoxyCodeLine{1156 \textcolor{comment}{                                 !! a layer's temperature change to the change in column}}
\DoxyCodeLine{1157 \textcolor{comment}{                                 !! height, including all implicit diffusive changes}}
\DoxyCodeLine{1158 \textcolor{comment}{                                 !! in the temperatures of all the layers below [Z degC-\/1 ~> m degC-\/1].}}
\DoxyCodeLine{1159 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dS\_to\_dColHt\_k\textcolor{comment}{ !< A factor (mass\_lay*dSColHtc\_vol/dS) relating}}
\DoxyCodeLine{1160 \textcolor{comment}{                                 !! a layer's salinity change to the change in column}}
\DoxyCodeLine{1161 \textcolor{comment}{                                 !! height, including all implicit diffusive changes}}
\DoxyCodeLine{1162 \textcolor{comment}{                                 !! in the salinities of all the layers below [Z ppt-\/1 ~> m ppt-\/1].}}
\DoxyCodeLine{1163 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dT\_to\_dColHta\textcolor{comment}{ !< A factor (mass\_lay*dSColHtc\_vol/dT) relating}}
\DoxyCodeLine{1164 \textcolor{comment}{                                 !! a layer's temperature change to the change in column}}
\DoxyCodeLine{1165 \textcolor{comment}{                                 !! height, including all implicit diffusive changes}}
\DoxyCodeLine{1166 \textcolor{comment}{                                 !! in the temperatures of all the layers above [Z degC-\/1 ~> m degC-\/1].}}
\DoxyCodeLine{1167 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{intent(in)}  :: dS\_to\_dColHta\textcolor{comment}{ !< A factor (mass\_lay*dSColHtc\_vol/dS) relating}}
\DoxyCodeLine{1168 \textcolor{comment}{                                 !! a layer's salinity change to the change in column}}
\DoxyCodeLine{1169 \textcolor{comment}{                                 !! height, including all implicit diffusive changes}}
\DoxyCodeLine{1170 \textcolor{comment}{                                 !! in the salinities of all the layers above [Z ppt-\/1 ~> m ppt-\/1].}}
\DoxyCodeLine{1171 }
\DoxyCodeLine{1172 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: PE\_chg\textcolor{comment}{   !< The change in column potential energy from applying}}
\DoxyCodeLine{1173 \textcolor{comment}{                                          !! Kddt\_h at the present interface [R Z L2 T-\/2 ~> J m-\/2].}}
\DoxyCodeLine{1174 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: dPEc\_dKd\textcolor{comment}{ !< The partial derivative of PE\_chg with Kddt\_h,}}
\DoxyCodeLine{1175 \textcolor{comment}{                                          !! [R Z L2 T-\/2 H-\/1 ~> J m-\/3 or J kg-\/1].}}
\DoxyCodeLine{1176 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: dPE\_max\textcolor{comment}{  !< The maximum change in column potential energy that could}}
\DoxyCodeLine{1177 \textcolor{comment}{                                          !! be realized by applying a huge value of Kddt\_h at the}}
\DoxyCodeLine{1178 \textcolor{comment}{                                          !! present interface [R Z L2 T-\/2 ~> J m-\/2].}}
\DoxyCodeLine{1179 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: dPEc\_dKd\_0\textcolor{comment}{ !< The partial derivative of PE\_chg with Kddt\_h in the}}
\DoxyCodeLine{1180 \textcolor{comment}{                                            !! limit where Kddt\_h = 0 [R Z L2 T-\/2 H-\/1 ~> J m-\/3 or J kg-\/1].}}
\DoxyCodeLine{1181 }
\DoxyCodeLine{1182 \textcolor{comment}{!   This subroutine determines the total potential energy change due to mixing}}
\DoxyCodeLine{1183 \textcolor{comment}{! at an interface, including all of the implicit effects of the prescribed}}
\DoxyCodeLine{1184 \textcolor{comment}{! mixing at interfaces above.  Everything here is derived by careful manipulation}}
\DoxyCodeLine{1185 \textcolor{comment}{! of the robust tridiagonal solvers used for tracers by MOM6.  The results are}}
\DoxyCodeLine{1186 \textcolor{comment}{! positive for mixing in a stably stratified environment.}}
\DoxyCodeLine{1187 \textcolor{comment}{!   The comments describing these arguments are for a downward mixing pass, but}}
\DoxyCodeLine{1188 \textcolor{comment}{! this routine can also be used for an upward pass with the sense of direction}}
\DoxyCodeLine{1189 \textcolor{comment}{! reversed.}}
\DoxyCodeLine{1190 }
\DoxyCodeLine{1191 \textcolor{keywordtype}{  real} :: b1            \textcolor{comment}{! b1 is used by the tridiagonal solver [H-\/1 ~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{1192 \textcolor{keywordtype}{  real} :: b1Kd          \textcolor{comment}{! Temporary array [nondim]}}
\DoxyCodeLine{1193 \textcolor{keywordtype}{  real} :: ColHt\_chg     \textcolor{comment}{! The change in column thickness [Z ~> m].}}
\DoxyCodeLine{1194 \textcolor{keywordtype}{  real} :: dColHt\_max    \textcolor{comment}{! The change in column thickness for infinite diffusivity [Z ~> m].}}
\DoxyCodeLine{1195 \textcolor{keywordtype}{  real} :: dColHt\_dKd    \textcolor{comment}{! The partial derivative of column thickness with Kddt\_h [Z H-\/1 ~> 1 or m3 kg-\/1].}}
\DoxyCodeLine{1196 \textcolor{keywordtype}{  real} :: dT\_k, dT\_km1  \textcolor{comment}{! Temporary arrays [degC].}}
\DoxyCodeLine{1197 \textcolor{keywordtype}{  real} :: dS\_k, dS\_km1  \textcolor{comment}{! Temporary arrays [ppt].}}
\DoxyCodeLine{1198 \textcolor{keywordtype}{  real} :: I\_Kr\_denom    \textcolor{comment}{! Temporary arrays [H-\/2 ~> m-\/2 or m4 kg-\/2].}}
\DoxyCodeLine{1199 \textcolor{keywordtype}{  real} :: dKr\_dKd       \textcolor{comment}{! Nondimensional temporary array [nondim].}}
\DoxyCodeLine{1200 \textcolor{keywordtype}{  real} :: ddT\_k\_dKd, ddT\_km1\_dKd \textcolor{comment}{! Temporary arrays [degC H-\/1 ~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{1201 \textcolor{keywordtype}{  real} :: ddS\_k\_dKd, ddS\_km1\_dKd \textcolor{comment}{! Temporary arrays [ppt H-\/1 ~> ppt m-\/1 or ppt m2 kg-\/1].}}
\DoxyCodeLine{1202 }
\DoxyCodeLine{1203   b1 = 1.0 / (b\_den\_1 + kddt\_h)}
\DoxyCodeLine{1204   b1kd = kddt\_h*b1}
\DoxyCodeLine{1205 }
\DoxyCodeLine{1206   \textcolor{comment}{! Start with the temperature change in layer k-\/1 due to the diffusivity at}}
\DoxyCodeLine{1207   \textcolor{comment}{! interface K without considering the effects of changes in layer k.}}
\DoxyCodeLine{1208 }
\DoxyCodeLine{1209   \textcolor{comment}{! Calculate the change in PE due to the diffusion at interface K}}
\DoxyCodeLine{1210   \textcolor{comment}{! if Kddt\_h(K+1) = 0.}}
\DoxyCodeLine{1211   i\_kr\_denom = 1.0 / (h\_k*b\_den\_1 + (b\_den\_1 + h\_k)*kddt\_h)}
\DoxyCodeLine{1212 }
\DoxyCodeLine{1213   dt\_k = (kddt\_h*i\_kr\_denom) * dte\_term}
\DoxyCodeLine{1214   ds\_k = (kddt\_h*i\_kr\_denom) * dse\_term}
\DoxyCodeLine{1215 }
\DoxyCodeLine{1216   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(pe\_chg)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1217     \textcolor{comment}{! Find the change in energy due to diffusion with strength Kddt\_h at this interface.}}
\DoxyCodeLine{1218     \textcolor{comment}{! Increment the temperature changes in layer k-\/1 due the changes in layer k.}}
\DoxyCodeLine{1219     dt\_km1 = b1kd * ( dt\_k + dt\_km1\_t2 )}
\DoxyCodeLine{1220     ds\_km1 = b1kd * ( ds\_k + ds\_km1\_t2 )}
\DoxyCodeLine{1221 }
\DoxyCodeLine{1222     pe\_chg = (dt\_to\_dpe\_k * dt\_k + dt\_to\_dpea * dt\_km1) + \&}
\DoxyCodeLine{1223              (ds\_to\_dpe\_k * ds\_k + ds\_to\_dpea * ds\_km1)}
\DoxyCodeLine{1224     colht\_chg = (dt\_to\_dcolht\_k * dt\_k + dt\_to\_dcolhta * dt\_km1) + \&}
\DoxyCodeLine{1225                 (ds\_to\_dcolht\_k * ds\_k + ds\_to\_dcolhta * ds\_km1)}
\DoxyCodeLine{1226     \textcolor{keywordflow}{if} (colht\_chg < 0.0) pe\_chg = pe\_chg -\/ pres\_z * colht\_chg}
\DoxyCodeLine{1227 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1228 }
\DoxyCodeLine{1229   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dpec\_dkd)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1230     \textcolor{comment}{! Find the derivatives of the temperature and salinity changes with Kddt\_h.}}
\DoxyCodeLine{1231     dkr\_dkd = (h\_k*b\_den\_1) * i\_kr\_denom**2}
\DoxyCodeLine{1232 }
\DoxyCodeLine{1233     ddt\_k\_dkd = dkr\_dkd * dte\_term}
\DoxyCodeLine{1234     dds\_k\_dkd = dkr\_dkd * dse\_term}
\DoxyCodeLine{1235     ddt\_km1\_dkd = (b1**2 * b\_den\_1) * ( dt\_k + dt\_km1\_t2 ) + b1kd * ddt\_k\_dkd}
\DoxyCodeLine{1236     dds\_km1\_dkd = (b1**2 * b\_den\_1) * ( ds\_k + ds\_km1\_t2 ) + b1kd * dds\_k\_dkd}
\DoxyCodeLine{1237 }
\DoxyCodeLine{1238     \textcolor{comment}{! Calculate the partial derivative of Pe\_chg with Kddt\_h.}}
\DoxyCodeLine{1239     dpec\_dkd = (dt\_to\_dpe\_k * ddt\_k\_dkd + dt\_to\_dpea * ddt\_km1\_dkd) + \&}
\DoxyCodeLine{1240                (ds\_to\_dpe\_k * dds\_k\_dkd + ds\_to\_dpea * dds\_km1\_dkd)}
\DoxyCodeLine{1241     dcolht\_dkd = (dt\_to\_dcolht\_k * ddt\_k\_dkd + dt\_to\_dcolhta * ddt\_km1\_dkd) + \&}
\DoxyCodeLine{1242                  (ds\_to\_dcolht\_k * dds\_k\_dkd + ds\_to\_dcolhta * dds\_km1\_dkd)}
\DoxyCodeLine{1243     \textcolor{keywordflow}{if} (dcolht\_dkd < 0.0) dpec\_dkd = dpec\_dkd -\/ pres\_z * dcolht\_dkd}
\DoxyCodeLine{1244 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1245 }
\DoxyCodeLine{1246   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dpe\_max)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1247     \textcolor{comment}{! This expression is the limit of PE\_chg for infinite Kddt\_h.}}
\DoxyCodeLine{1248     dpe\_max = (dt\_to\_dpea * dt\_km1\_t2 + ds\_to\_dpea * ds\_km1\_t2) + \&}
\DoxyCodeLine{1249               ((dt\_to\_dpe\_k + dt\_to\_dpea) * dte\_term + \&}
\DoxyCodeLine{1250                (ds\_to\_dpe\_k + ds\_to\_dpea) * dse\_term) / (b\_den\_1 + h\_k)}
\DoxyCodeLine{1251     dcolht\_max = (dt\_to\_dcolhta * dt\_km1\_t2 + ds\_to\_dcolhta * ds\_km1\_t2) + \&}
\DoxyCodeLine{1252               ((dt\_to\_dcolht\_k + dt\_to\_dcolhta) * dte\_term + \&}
\DoxyCodeLine{1253                (ds\_to\_dcolht\_k + ds\_to\_dcolhta) * dse\_term) / (b\_den\_1 + h\_k)}
\DoxyCodeLine{1254     \textcolor{keywordflow}{if} (dcolht\_max < 0.0) dpe\_max = dpe\_max -\/ pres\_z*dcolht\_max}
\DoxyCodeLine{1255 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1256 }
\DoxyCodeLine{1257   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dpec\_dkd\_0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1258     \textcolor{comment}{! This expression is the limit of dPEc\_dKd for Kddt\_h = 0.}}
\DoxyCodeLine{1259     dpec\_dkd\_0 = (dt\_to\_dpea * dt\_km1\_t2 + ds\_to\_dpea * ds\_km1\_t2) / (b\_den\_1) + \&}
\DoxyCodeLine{1260                  (dt\_to\_dpe\_k * dte\_term + ds\_to\_dpe\_k * dse\_term) / (h\_k*b\_den\_1)}
\DoxyCodeLine{1261     dcolht\_dkd = (dt\_to\_dcolhta * dt\_km1\_t2 + ds\_to\_dcolhta * ds\_km1\_t2) / (b\_den\_1) + \&}
\DoxyCodeLine{1262                  (dt\_to\_dcolht\_k * dte\_term + ds\_to\_dcolht\_k * dse\_term) / (h\_k*b\_den\_1)}
\DoxyCodeLine{1263     \textcolor{keywordflow}{if} (dcolht\_dkd < 0.0) dpec\_dkd\_0 = dpec\_dkd\_0 -\/ pres\_z*dcolht\_dkd}
\DoxyCodeLine{1264 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1265 }

\end{DoxyCode}
