\hypertarget{namespacemom__diapyc__energy__req}{}\section{mom\+\_\+diapyc\+\_\+energy\+\_\+req Module Reference}
\label{namespacemom__diapyc__energy__req}\index{mom\+\_\+diapyc\+\_\+energy\+\_\+req@{mom\+\_\+diapyc\+\_\+energy\+\_\+req}}


\subsection{Detailed Description}
Calculates the energy requirements of mixing. \subsection*{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}
\subsection*{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}


\subsection{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}}
\subsubsection{\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{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em h\+\_\+in} & Layer thickness before entrainment, \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em t\+\_\+in} & The layer temperatures \mbox{[}degC\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em s\+\_\+in} & The layer salinities \mbox{[}ppt\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em kd} & The interfaces diapycnal diffusivities \mbox{[}Z2 T-\/1 $\sim$$>$ m2 s-\/1\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em dt} & The amount of time covered by this call \mbox{[}T $\sim$$>$ s\mbox{]}.\\
\hline
\mbox{\tt 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{\tt in,out}  & {\em tv} & A structure containing pointers to any available thermodynamic fields. Absent fields have N\+U\+LL ptrs.\\
\hline
\mbox{\tt 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}
122   \textcolor{keywordtype}{type}(ocean\_grid\_type),    \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{    !< The ocean's grid structure.}
123   \textcolor{keywordtype}{type}(verticalGrid\_type),  \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure.}
124   \textcolor{keywordtype}{type}(unit\_scale\_type),    \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{   !< A dimensional unit scaling type}
125   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(GV%ke)},   \textcolor{keywordtype}{intent(in)}    :: h\_in\textcolor{comment}{ !< Layer thickness before entrainment,}
126 \textcolor{comment}{                                                  !! [H ~> m or kg m-2].}
127   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(GV%ke)},   \textcolor{keywordtype}{intent(in)}    :: T\_in\textcolor{comment}{ !< The layer temperatures [degC].}
128   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(GV%ke)},   \textcolor{keywordtype}{intent(in)}    :: S\_in\textcolor{comment}{ !< The layer salinities [ppt].}
129   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(GV%ke+1)}, \textcolor{keywordtype}{intent(in)}    :: Kd\textcolor{comment}{   !< The interfaces diapycnal diffusivities}
130 \textcolor{comment}{                                                  !! [Z2 T-1 ~> m2 s-1].}
131   \textcolor{keywordtype}{real},                     \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{   !< The amount of time covered by this call [T ~> s].}
132   \textcolor{keywordtype}{real},                     \textcolor{keywordtype}{intent(out)}   :: energy\_Kd\textcolor{comment}{ !< The column-integrated rate of energy}
133 \textcolor{comment}{                                                  !! consumption by diapycnal diffusion [R Z L2 T-3 ~> W
       m-2].}
134   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),    \textcolor{keywordtype}{intent(inout)} :: tv\textcolor{comment}{   !< A structure containing pointers to any}
135 \textcolor{comment}{                                                  !! available thermodynamic fields.}
136 \textcolor{comment}{                                                  !! Absent fields have NULL ptrs.}
137   \textcolor{keywordtype}{logical},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: may\_print\textcolor{comment}{ !< If present and true, write out diagnostics}
138 \textcolor{comment}{                                                  !! of energy use.}
139   \textcolor{keywordtype}{type}(diapyc\_energy\_req\_CS), &
140                   \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{   !< This module's control structure.}
141 
142 \textcolor{comment}{!   This subroutine uses a substantially refactored tridiagonal equation for}
143 \textcolor{comment}{! diapycnal mixing of temperature and salinity to estimate the potential energy}
144 \textcolor{comment}{! change due to diapycnal mixing in a column of water.  It does this estimate}
145 \textcolor{comment}{! 4 different ways, all of which should be equivalent, but reports only one.}
146 \textcolor{comment}{! The various estimates are taken because they will later be used as templates}
147 \textcolor{comment}{! for other bits of code.}
148 
149   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(GV%ke)} :: &
150     p\_lay, &    \textcolor{comment}{! Average pressure of a layer [R L2 T-2 ~> Pa].}
151     dSV\_dT, &   \textcolor{comment}{! Partial derivative of specific volume with temperature [R-1 degC-1 ~> m3 kg-1 degC-1].}
152     dSV\_dS, &   \textcolor{comment}{! Partial derivative of specific volume with salinity [R-1 ppt-1 ~> m3 kg-1 ppt-1].}
153     T0, S0, &   \textcolor{comment}{! Initial temperatures and salinities [degC] and [ppt].}
154     Te, Se, &   \textcolor{comment}{! Running incomplete estimates of the new temperatures and salinities [degC] and [ppt].}
155     Te\_a, Se\_a, & \textcolor{comment}{! Running incomplete estimates of the new temperatures and salinities [degC] and [ppt].}
156     Te\_b, Se\_b, & \textcolor{comment}{! Running incomplete estimates of the new temperatures and salinities [degC] and [ppt].}
157     Tf, Sf, &   \textcolor{comment}{! New final values of the temperatures and salinities [degC] and [ppt].}
158     dTe, dSe, & \textcolor{comment}{! Running (1-way) estimates of temperature and salinity change [degC] and [ppt].}
159     dTe\_a, dSe\_a, & \textcolor{comment}{! Running (1-way) estimates of temperature and salinity change [degC] and [ppt].}
160     dTe\_b, dSe\_b, & \textcolor{comment}{! Running (1-way) estimates of temperature and salinity change [degC] and [ppt].}
161     Th\_a, &     \textcolor{comment}{! An effective temperature times a thickness in the layer above, including implicit}
162                 \textcolor{comment}{! mixing effects with other yet higher layers [degC H ~> degC m or degC kg m-2].}
163     sh\_a, &     \textcolor{comment}{! An effective salinity times a thickness in the layer above, including implicit}
164                 \textcolor{comment}{! mixing effects with other yet higher layers [ppt H ~> ppt m or ppt kg m-2].}
165     th\_b, &     \textcolor{comment}{! An effective temperature times a thickness in the layer below, including implicit}
166                 \textcolor{comment}{! mixing effects with other yet lower layers [degC H ~> degC m or degC kg m-2].}
167     sh\_b, &     \textcolor{comment}{! An effective salinity times a thickness in the layer below, including implicit}
168                 \textcolor{comment}{! mixing effects with other yet lower layers [ppt H ~> ppt m or ppt kg m-2].}
169     dt\_to\_dpe, & \textcolor{comment}{! Partial derivative of column potential energy with the temperature and salinity}
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].}
171     dt\_to\_dcolht, & \textcolor{comment}{! Partial derivatives of the total column height with the temperature}
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].}
173     dt\_to\_dcolht\_a, & \textcolor{comment}{! Partial derivatives of the total column height with the temperature}
174     ds\_to\_dcolht\_a, & \textcolor{comment}{! and salinity changes within a layer, including the implicit effects}
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].}
176     dt\_to\_dcolht\_b, & \textcolor{comment}{! Partial derivatives of the total column height with the temperature}
177     ds\_to\_dcolht\_b, & \textcolor{comment}{! and salinity changes within a layer, including the implicit effects}
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].}
179     dt\_to\_dpe\_a, &  \textcolor{comment}{! Partial derivatives of column potential energy with the temperature}
180     ds\_to\_dpe\_a, &  \textcolor{comment}{! and salinity changes within a layer, including the implicit effects}
181                     \textcolor{comment}{! of mixing with layers higher in the water column, in}
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].}
183     dt\_to\_dpe\_b, &  \textcolor{comment}{! Partial derivatives of column potential energy with the temperature}
184     ds\_to\_dpe\_b, &  \textcolor{comment}{! and salinity changes within a layer, including the implicit effects}
185                     \textcolor{comment}{! of mixing with layers lower in the water column, in}
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].}
187     hp\_a, &     \textcolor{comment}{! An effective pivot thickness of the layer including the effects}
188                 \textcolor{comment}{! of coupling with layers above [H ~> m or kg m-2].  This is the first term}
189                 \textcolor{comment}{! in the denominator of b1 in a downward-oriented tridiagonal solver.}
190     hp\_b, &     \textcolor{comment}{! An effective pivot thickness of the layer including the effects}
191                 \textcolor{comment}{! of coupling with layers below [H ~> m or kg m-2].  This is the first term}
192                 \textcolor{comment}{! in the denominator of b1 in an upward-oriented tridiagonal solver.}
193     c1\_a, &     \textcolor{comment}{! c1\_a is used by a downward-oriented tridiagonal solver [nondim].}
194     c1\_b, &     \textcolor{comment}{! c1\_b is used by an upward-oriented tridiagonal solver [nondim].}
195     h\_tr        \textcolor{comment}{! h\_tr is h at tracer points with a h\_neglect added to}
196                 \textcolor{comment}{! ensure positive definiteness [H ~> m or kg m-2].}
197   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(GV%ke+1)} :: &
198     pres, &     \textcolor{comment}{! Interface pressures [R L2 T-2 ~> Pa].}
199     pres\_Z, &   \textcolor{comment}{! Interface pressures with a rescaling factor to convert interface height}
200                 \textcolor{comment}{! movements into changes in column potential energy [R L2 T-2 m Z-1 ~> J m-3].}
201     z\_int, &    \textcolor{comment}{! Interface heights relative to the surface [H ~> m or kg m-2].}
202     n2, &       \textcolor{comment}{! An estimate of the buoyancy frequency [T-2 ~> s-2].}
203     kddt\_h, &   \textcolor{comment}{! The diapycnal diffusivity times a timestep divided by the}
204                 \textcolor{comment}{! average thicknesses around a layer [H ~> m or kg m-2].}
205     kddt\_h\_a, & \textcolor{comment}{! The value of Kddt\_h for layers above the central point in the}
206                 \textcolor{comment}{! tridiagonal solver [H ~> m or kg m-2].}
207     kddt\_h\_b, & \textcolor{comment}{! The value of Kddt\_h for layers below the central point in the}
208                 \textcolor{comment}{! tridiagonal solver [H ~> m or kg m-2].}
209     kd\_so\_far   \textcolor{comment}{! The value of Kddt\_h that has been applied already in}
210                 \textcolor{comment}{! calculating the energy changes [H ~> m or kg m-2].}
211   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(GV%ke+1,4)} :: &
212     PE\_chg\_k, &     \textcolor{comment}{! The integrated potential energy change within a timestep due}
213                     \textcolor{comment}{! to the diffusivity at interface K for 4 different orders of}
214                     \textcolor{comment}{! accumulating the diffusivities [R Z L2 T-2 ~> J m-2].}
215     colht\_cor\_k     \textcolor{comment}{! The correction to the potential energy change due to}
216                     \textcolor{comment}{! changes in the net column height [R Z L2 T-2 ~> J m-2].}
217   \textcolor{keywordtype}{real} :: &
218     b1              \textcolor{comment}{! b1 is used by the tridiagonal solver [H-1 ~> m-1 or m2 kg-1].}
219   \textcolor{keywordtype}{real} :: &
220     I\_b1            \textcolor{comment}{! The inverse of b1 [H ~> m or kg m-2].}
221   \textcolor{keywordtype}{real} :: Kd0       \textcolor{comment}{! The value of Kddt\_h that has already been applied [H ~> m or kg m-2].}
222   \textcolor{keywordtype}{real} :: dKd       \textcolor{comment}{! The change in the value of Kddt\_h [H ~> m or kg m-2].}
223   \textcolor{keywordtype}{real} :: h\_neglect \textcolor{comment}{! A thickness that is so small it is usually lost}
224                     \textcolor{comment}{! in roundoff and can be neglected [H ~> m or kg m-2].}
225   \textcolor{keywordtype}{real} :: dTe\_term  \textcolor{comment}{! A diffusivity-independent term related to the temperature}
226                     \textcolor{comment}{! change in the layer below the interface [degC H ~> degC m or degC kg m-2].}
227   \textcolor{keywordtype}{real} :: dSe\_term  \textcolor{comment}{! A diffusivity-independent term related to the salinity}
228                     \textcolor{comment}{! change in the layer below the interface [ppt H ~> ppt m or ppt kg m-2].}
229   \textcolor{keywordtype}{real} :: Kddt\_h\_guess \textcolor{comment}{! A guess of the final value of Kddt\_h [H ~> m or kg m-2].}
230   \textcolor{keywordtype}{real} :: dMass     \textcolor{comment}{! The mass per unit area within a layer [R Z ~> kg m-2].}
231   \textcolor{keywordtype}{real} :: dPres     \textcolor{comment}{! The hydrostatic pressure change across a layer [R L2 T-2 ~> Pa].}
232   \textcolor{keywordtype}{real} :: dMKE\_max  \textcolor{comment}{! The maximum amount of mean kinetic energy that could be}
233                     \textcolor{comment}{! converted to turbulent kinetic energy if the velocity in}
234                     \textcolor{comment}{! the layer below an interface were homogenized with all of}
235                     \textcolor{comment}{! the water above the interface [R Z L2 T-2 ~> J m-2 = kg s-2].}
236   \textcolor{keywordtype}{real} :: rho\_here  \textcolor{comment}{! The in-situ density [R ~> kg m-3].}
237   \textcolor{keywordtype}{real} :: PE\_change \textcolor{comment}{! The change in column potential energy from applying Kddt\_h at the}
238                     \textcolor{comment}{! present interface [R L2 Z T-2 ~> J m-2].}
239   \textcolor{keywordtype}{real} :: ColHt\_cor \textcolor{comment}{! The correction to PE\_chg that is made due to a net}
240                     \textcolor{comment}{! change in the column height [R L2 Z T-2 ~> J m-2].}
241   \textcolor{keywordtype}{real} :: htot      \textcolor{comment}{! A running sum of thicknesses [H ~> m or kg m-2].}
242   \textcolor{keywordtype}{real} :: dTe\_t2, dT\_km1\_t2, dT\_k\_t2  \textcolor{comment}{! Temporary arrays describing temperature changes [degC].}
243   \textcolor{keywordtype}{real} :: dSe\_t2, dS\_km1\_t2, dS\_k\_t2  \textcolor{comment}{! Temporary arrays describing salinity changes [ppt].}
244   \textcolor{keywordtype}{logical} :: do\_print
245 
246   \textcolor{comment}{! The following are a bunch of diagnostic arrays for debugging purposes.}
247   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(GV%ke)} :: &
248     Ta, Sa, Tb, Sb
249   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(GV%ke+1)} :: &
250     dPEa\_dKd, dPEa\_dKd\_est, dPEa\_dKd\_err, dPEa\_dKd\_trunc, dPEa\_dKd\_err\_norm, &
251     dPEb\_dKd, dPEb\_dKd\_est, dPEb\_dKd\_err, dPEb\_dKd\_trunc, dPEb\_dKd\_err\_norm
252   \textcolor{keywordtype}{real} :: PE\_chg\_tot1A, PE\_chg\_tot2A, T\_chg\_totA
253   \textcolor{keywordtype}{real} :: PE\_chg\_tot1B, PE\_chg\_tot2B, T\_chg\_totB
254   \textcolor{keywordtype}{real} :: PE\_chg\_tot1C, PE\_chg\_tot2C, T\_chg\_totC
255   \textcolor{keywordtype}{real} :: PE\_chg\_tot1D, PE\_chg\_tot2D, T\_chg\_totD
256   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(GV%ke+1)}  :: dPEchg\_dKd
257   \textcolor{keywordtype}{real} :: PE\_chg(6)
258   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(6)} :: dT\_k\_itt, dS\_k\_itt, dT\_km1\_itt, dS\_km1\_itt
259 
260   \textcolor{keywordtype}{integer} :: k, nz, itt, max\_itt, k\_cent
261   \textcolor{keywordtype}{logical} :: surface\_BL, bottom\_BL, central, halves, debug
262   \textcolor{keywordtype}{logical} :: old\_PE\_calc
263   nz = g%ke
264   h\_neglect = gv%H\_subroundoff
265 
266   debug = .true.
267 
268   surface\_bl = .true. ; bottom\_bl = .true. ; halves = .true.
269   central = .true. ; k\_cent = nz/2
270 
271   do\_print = .false. ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(may\_print) .and. \textcolor{keyword}{present}(cs)) do\_print = may\_print
272 
273   dpea\_dkd(:) = 0.0 ; dpea\_dkd\_est(:) = 0.0 ; dpea\_dkd\_err(:) = 0.0
274   dpea\_dkd\_err\_norm(:) = 0.0 ; dpea\_dkd\_trunc(:) = 0.0
275   dpeb\_dkd(:) = 0.0 ; dpeb\_dkd\_est(:) = 0.0 ; dpeb\_dkd\_err(:) = 0.0
276   dpeb\_dkd\_err\_norm(:) = 0.0 ; dpeb\_dkd\_trunc(:) = 0.0
277 
278   htot = 0.0 ; pres(1) = 0.0 ; pres\_z(1) = 0.0 ; z\_int(1) = 0.0
279   \textcolor{keywordflow}{do} k=1,nz
280     t0(k) = t\_in(k) ; s0(k) = s\_in(k)
281     h\_tr(k) = h\_in(k)
282     htot = htot + h\_tr(k)
283     pres(k+1) = pres(k) + (gv%g\_Earth * gv%H\_to\_RZ) * h\_tr(k)
284     pres\_z(k+1) = pres(k+1)
285     p\_lay(k) = 0.5*(pres(k) + pres(k+1))
286     z\_int(k+1) = z\_int(k) - h\_tr(k)
287 \textcolor{keywordflow}{  enddo}
288   \textcolor{keywordflow}{do} k=1,nz
289     h\_tr(k) = max(h\_tr(k), 1e-15*htot)
290 \textcolor{keywordflow}{  enddo}
291 
292   \textcolor{comment}{! Introduce a diffusive flux variable, Kddt\_h(K) = ea(k) = eb(k-1)}
293 
294   kddt\_h(1) = 0.0 ; kddt\_h(nz+1) = 0.0
295   \textcolor{keywordflow}{do} k=2,nz
296     kddt\_h(k) = min((gv%Z\_to\_H**2*dt)*kd(k) / (0.5*(h\_tr(k-1) + h\_tr(k))), 1e3*htot)
297 \textcolor{keywordflow}{  enddo}
298 
299   \textcolor{comment}{! Solve the tridiagonal equations for new temperatures.}
300 
301   \textcolor{keyword}{call }calculate\_specific\_vol\_derivs(t0, s0, p\_lay, dsv\_dt, dsv\_ds, tv%eqn\_of\_state)
302 
303   \textcolor{keywordflow}{do} k=1,nz
304     dmass = gv%H\_to\_RZ * h\_tr(k)
305     dpres = (gv%g\_Earth * gv%H\_to\_RZ) * h\_tr(k)
306     dt\_to\_dpe(k) = (dmass * (pres(k) + 0.5*dpres)) * dsv\_dt(k)
307     ds\_to\_dpe(k) = (dmass * (pres(k) + 0.5*dpres)) * dsv\_ds(k)
308     dt\_to\_dcolht(k) = dmass * dsv\_dt(k) * cs%ColHt\_scaling
309     ds\_to\_dcolht(k) = dmass * dsv\_ds(k) * cs%ColHt\_scaling
310 \textcolor{keywordflow}{  enddo}
311 
312 \textcolor{comment}{!  PE\_chg\_k(1) = 0.0 ; PE\_chg\_k(nz+1) = 0.0}
313   \textcolor{comment}{! PEchg(:) = 0.0}
314   pe\_chg\_k(:,:) = 0.0 ; colht\_cor\_k(:,:) = 0.0
315   dpechg\_dkd(:) = 0.0
316 
317   \textcolor{keywordflow}{if} (surface\_bl) \textcolor{keywordflow}{then}  \textcolor{comment}{! This version is appropriate for a surface boundary layer.}
318     old\_pe\_calc = .false.
319 
320     \textcolor{comment}{! Set up values appropriate for no diffusivity.}
321     \textcolor{keywordflow}{do} k=1,nz
322       hp\_a(k) = h\_tr(k) ; hp\_b(k) = h\_tr(k)
323       dt\_to\_dpe\_a(k) = dt\_to\_dpe(k) ; ds\_to\_dpe\_a(k) = ds\_to\_dpe(k)
324       dt\_to\_dpe\_b(k) = dt\_to\_dpe(k) ; ds\_to\_dpe\_b(k) = ds\_to\_dpe(k)
325       dt\_to\_dcolht\_a(k) = dt\_to\_dcolht(k) ; ds\_to\_dcolht\_a(k) = ds\_to\_dcolht(k)
326       dt\_to\_dcolht\_b(k) = dt\_to\_dcolht(k) ; ds\_to\_dcolht\_b(k) = ds\_to\_dcolht(k)
327 \textcolor{keywordflow}{    enddo}
328 
329     \textcolor{keywordflow}{do} k=2,nz
330       \textcolor{comment}{! At this point, Kddt\_h(K) will be unknown because its value may depend}
331       \textcolor{comment}{! on how much energy is available.}
332 
333       \textcolor{comment}{! Precalculate some temporary expressions that are independent of Kddt\_h\_guess.}
334       \textcolor{keywordflow}{if} (old\_pe\_calc) \textcolor{keywordflow}{then}
335         \textcolor{keywordflow}{if} (k==2) \textcolor{keywordflow}{then}
336           dt\_km1\_t2 = (t0(k)-t0(k-1))
337           ds\_km1\_t2 = (s0(k)-s0(k-1))
338           dte\_t2 = 0.0 ; dse\_t2 = 0.0
339         \textcolor{keywordflow}{else}
340           dte\_t2 = kddt\_h(k-1) * ((t0(k-2) - t0(k-1)) + dte(k-2))
341           dse\_t2 = kddt\_h(k-1) * ((s0(k-2) - s0(k-1)) + dse(k-2))
342           dt\_km1\_t2 = (t0(k)-t0(k-1)) - &
343                 (kddt\_h(k-1) / hp\_a(k-1)) * ((t0(k-2) - t0(k-1)) + dte(k-2))
344           ds\_km1\_t2 = (s0(k)-s0(k-1)) - &
345                 (kddt\_h(k-1) / hp\_a(k-1)) * ((s0(k-2) - s0(k-1)) + dse(k-2))
346 \textcolor{keywordflow}{        endif}
347         dte\_term = dte\_t2 + hp\_a(k-1) * (t0(k-1)-t0(k))
348         dse\_term = dse\_t2 + hp\_a(k-1) * (s0(k-1)-s0(k))
349       \textcolor{keywordflow}{else}
350         \textcolor{keywordflow}{if} (k<=2) \textcolor{keywordflow}{then}
351           th\_a(k-1) = h\_tr(k-1) * t0(k-1) ; sh\_a(k-1) = h\_tr(k-1) * s0(k-1)
352         \textcolor{keywordflow}{else}
353           th\_a(k-1) = h\_tr(k-1) * t0(k-1) + kddt\_h(k-1) * te(k-2)
354           sh\_a(k-1) = h\_tr(k-1) * s0(k-1) + kddt\_h(k-1) * se(k-2)
355 \textcolor{keywordflow}{        endif}
356         th\_b(k) = h\_tr(k) * t0(k) ; sh\_b(k) = h\_tr(k) * s0(k)
357 \textcolor{keywordflow}{      endif}
358 
359 
360       \textcolor{comment}{! Find the energy change due to a guess at the strength of diffusion at interface K.}
361 
362       kddt\_h\_guess = kddt\_h(k)
363       \textcolor{keywordflow}{if} (old\_pe\_calc) \textcolor{keywordflow}{then}
364         \textcolor{keyword}{call }find\_pe\_chg\_orig(kddt\_h\_guess, h\_tr(k), hp\_a(k-1), &
365                          dte\_term, dse\_term, dt\_km1\_t2, ds\_km1\_t2, &
366                          dt\_to\_dpe(k), ds\_to\_dpe(k), dt\_to\_dpe\_a(k-1), ds\_to\_dpe\_a(k-1), &
367                          pres\_z(k), dt\_to\_dcolht(k), ds\_to\_dcolht(k), &
368                          dt\_to\_dcolht\_a(k-1), ds\_to\_dcolht\_a(k-1), &
369                          pe\_chg\_k(k,1), dpea\_dkd(k))
370       \textcolor{keywordflow}{else}
371         \textcolor{keyword}{call }find\_pe\_chg(0.0, kddt\_h\_guess, hp\_a(k-1), hp\_b(k), &
372                          th\_a(k-1), sh\_a(k-1), th\_b(k), sh\_b(k), &
373                          dt\_to\_dpe\_a(k-1), ds\_to\_dpe\_a(k-1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), &
374                          pres\_z(k), dt\_to\_dcolht\_a(k-1), ds\_to\_dcolht\_a(k-1), &
375                          dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), &
376                          pe\_chg=pe\_chg\_k(k,1), dpec\_dkd=dpea\_dkd(k), &
377                          colht\_cor=colht\_cor\_k(k,1))
378 \textcolor{keywordflow}{      endif}
379 
380       \textcolor{keywordflow}{if} (debug) \textcolor{keywordflow}{then}
381         \textcolor{keywordflow}{do} itt=1,5
382           kddt\_h\_guess = (1.0+0.01*(itt-3))*kddt\_h(k)
383 
384           \textcolor{keywordflow}{if} (old\_pe\_calc) \textcolor{keywordflow}{then}
385             \textcolor{keyword}{call }find\_pe\_chg\_orig(kddt\_h\_guess, h\_tr(k), hp\_a(k-1), &
386                              dte\_term, dse\_term, dt\_km1\_t2, ds\_km1\_t2, &
387                              dt\_to\_dpe(k), ds\_to\_dpe(k), dt\_to\_dpe\_a(k-1), ds\_to\_dpe\_a(k-1), &
388                              pres\_z(k), dt\_to\_dcolht(k), ds\_to\_dcolht(k), &
389                              dt\_to\_dcolht\_a(k-1), ds\_to\_dcolht\_a(k-1), &
390                              pe\_chg=pe\_chg(itt))
391           \textcolor{keywordflow}{else}
392             \textcolor{keyword}{call }find\_pe\_chg(0.0, kddt\_h\_guess, hp\_a(k-1), hp\_b(k), &
393                              th\_a(k-1), sh\_a(k-1), th\_b(k), sh\_b(k), &
394                              dt\_to\_dpe\_a(k-1), ds\_to\_dpe\_a(k-1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), &
395                              pres\_z(k), dt\_to\_dcolht\_a(k-1), ds\_to\_dcolht\_a(k-1), &
396                              dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), &
397                              pe\_chg=pe\_chg(itt))
398 \textcolor{keywordflow}{          endif}
399 \textcolor{keywordflow}{        enddo}
400         \textcolor{comment}{! Compare with a 4th-order finite difference estimate.}
401         dpea\_dkd\_est(k) = (4.0*(pe\_chg(4)-pe\_chg(2))/(0.02*kddt\_h(k)) - &
402                                (pe\_chg(5)-pe\_chg(1))/(0.04*kddt\_h(k))) / 3.0
403         dpea\_dkd\_trunc(k) = (pe\_chg(4)-pe\_chg(2))/(0.02*kddt\_h(k)) - &
404                             (pe\_chg(5)-pe\_chg(1))/(0.04*kddt\_h(k))
405         dpea\_dkd\_err(k) = (dpea\_dkd\_est(k) - dpea\_dkd(k))
406         dpea\_dkd\_err\_norm(k) = (dpea\_dkd\_est(k) - dpea\_dkd(k)) / &
407                               (abs(dpea\_dkd\_est(k)) + abs(dpea\_dkd(k)) + 1e-100*us%RZ\_to\_kg\_m2*us
      %L\_T\_to\_m\_s**2)
408 \textcolor{keywordflow}{      endif}
409 
410       \textcolor{comment}{!   At this point, the final value of Kddt\_h(K) is known, so the estimated}
411       \textcolor{comment}{! properties for layer k-1 can be calculated.}
412 
413       b1 = 1.0 / (hp\_a(k-1) + kddt\_h(k))
414       c1\_a(k) = kddt\_h(k) * b1
415       \textcolor{keywordflow}{if} (k==2) \textcolor{keywordflow}{then}
416         te(1) = b1*(h\_tr(1)*t0(1))
417         se(1) = b1*(h\_tr(1)*s0(1))
418       \textcolor{keywordflow}{else}
419         te(k-1) = b1 * (h\_tr(k-1) * t0(k-1) + kddt\_h(k-1) * te(k-2))
420         se(k-1) = b1 * (h\_tr(k-1) * s0(k-1) + kddt\_h(k-1) * se(k-2))
421 \textcolor{keywordflow}{      endif}
422       \textcolor{keywordflow}{if} (old\_pe\_calc) \textcolor{keywordflow}{then}
423         dte(k-1) = b1 * ( kddt\_h(k)*(t0(k)-t0(k-1)) + dte\_t2 )
424         dse(k-1) = b1 * ( kddt\_h(k)*(s0(k)-s0(k-1)) + dse\_t2 )
425 \textcolor{keywordflow}{      endif}
426 
427       hp\_a(k) = h\_tr(k) + (hp\_a(k-1) * b1) * kddt\_h(k)
428       dt\_to\_dpe\_a(k) = dt\_to\_dpe(k) + c1\_a(k)*dt\_to\_dpe\_a(k-1)
429       ds\_to\_dpe\_a(k) = ds\_to\_dpe(k) + c1\_a(k)*ds\_to\_dpe\_a(k-1)
430       dt\_to\_dcolht\_a(k) = dt\_to\_dcolht(k) + c1\_a(k)*dt\_to\_dcolht\_a(k-1)
431       ds\_to\_dcolht\_a(k) = ds\_to\_dcolht(k) + c1\_a(k)*ds\_to\_dcolht\_a(k-1)
432 
433 \textcolor{keywordflow}{    enddo}
434 
435     b1 = 1.0 / (hp\_a(nz))
436     tf(nz) = b1 * (h\_tr(nz) * t0(nz) + kddt\_h(nz) * te(nz-1))
437     sf(nz) = b1 * (h\_tr(nz) * s0(nz) + kddt\_h(nz) * se(nz-1))
438     \textcolor{keywordflow}{if} (old\_pe\_calc) \textcolor{keywordflow}{then}
439       dte(nz) = b1 * kddt\_h(nz) * ((t0(nz-1)-t0(nz)) + dte(nz-1))
440       dse(nz) = b1 * kddt\_h(nz) * ((s0(nz-1)-s0(nz)) + dse(nz-1))
441 \textcolor{keywordflow}{    endif}
442 
443     \textcolor{keywordflow}{do} k=nz-1,1,-1
444       tf(k) = te(k) + c1\_a(k+1)*tf(k+1)
445       sf(k) = se(k) + c1\_a(k+1)*sf(k+1)
446 \textcolor{keywordflow}{    enddo}
447 
448     \textcolor{keywordflow}{if} (debug) \textcolor{keywordflow}{then}
449       \textcolor{keywordflow}{do} k=1,nz ; ta(k) = tf(k) ; sa(k) = sf(k) ;\textcolor{keywordflow}{ enddo}
450       pe\_chg\_tot1a = 0.0 ; pe\_chg\_tot2a = 0.0 ; t\_chg\_tota = 0.0
451       \textcolor{keywordflow}{do} k=1,nz
452         pe\_chg\_tot1a = pe\_chg\_tot1a + (dt\_to\_dpe(k) * (tf(k) - t0(k)) + &
453                                        ds\_to\_dpe(k) * (sf(k) - s0(k)))
454         t\_chg\_tota = t\_chg\_tota + h\_tr(k) * (tf(k) - t0(k))
455 \textcolor{keywordflow}{      enddo}
456       \textcolor{keywordflow}{do} k=2,nz
457         pe\_chg\_tot2a = pe\_chg\_tot2a + (pe\_chg\_k(k,1) - colht\_cor\_k(k,1))
458 \textcolor{keywordflow}{      enddo}
459 \textcolor{keywordflow}{    endif}
460 
461 \textcolor{keywordflow}{  endif}
462 
463   \textcolor{keywordflow}{if} (bottom\_bl) \textcolor{keywordflow}{then}  \textcolor{comment}{! This version is appropriate for a bottom boundary layer.}
464     old\_pe\_calc = .false.
465 
466     \textcolor{comment}{! Set up values appropriate for no diffusivity.}
467     \textcolor{keywordflow}{do} k=1,nz
468       hp\_a(k) = h\_tr(k) ; hp\_b(k) = h\_tr(k)
469       dt\_to\_dpe\_a(k) = dt\_to\_dpe(k) ; ds\_to\_dpe\_a(k) = ds\_to\_dpe(k)
470       dt\_to\_dpe\_b(k) = dt\_to\_dpe(k) ; ds\_to\_dpe\_b(k) = ds\_to\_dpe(k)
471       dt\_to\_dcolht\_a(k) = dt\_to\_dcolht(k) ; ds\_to\_dcolht\_a(k) = ds\_to\_dcolht(k)
472       dt\_to\_dcolht\_b(k) = dt\_to\_dcolht(k) ; ds\_to\_dcolht\_b(k) = ds\_to\_dcolht(k)
473 \textcolor{keywordflow}{    enddo}
474 
475     \textcolor{keywordflow}{do} k=nz,2,-1  \textcolor{comment}{! Loop over interior interfaces.}
476       \textcolor{comment}{! At this point, Kddt\_h(K) will be unknown because its value may depend}
477       \textcolor{comment}{! on how much energy is available.}
478 
479       \textcolor{comment}{! Precalculate some temporary expressions that are independent of Kddt\_h\_guess.}
480       \textcolor{keywordflow}{if} (old\_pe\_calc) \textcolor{keywordflow}{then}
481         \textcolor{keywordflow}{if} (k==nz) \textcolor{keywordflow}{then}
482           dt\_k\_t2 = (t0(k-1)-t0(k))
483           ds\_k\_t2 = (s0(k-1)-s0(k))
484           dte\_t2 = 0.0 ; dse\_t2 = 0.0
485         \textcolor{keywordflow}{else}
486           dte\_t2 = kddt\_h(k+1) * ((t0(k+1) - t0(k)) + dte(k+1))
487           dse\_t2 = kddt\_h(k+1) * ((s0(k+1) - s0(k)) + dse(k+1))
488           dt\_k\_t2 = (t0(k-1)-t0(k)) - &
489                   (kddt\_h(k+1)/ hp\_b(k)) * ((t0(k+1) - t0(k)) + dte(k+1))
490           ds\_k\_t2 = (s0(k-1)-s0(k)) - &
491                   (kddt\_h(k+1)/ hp\_b(k)) * ((s0(k+1) - s0(k)) + dse(k+1))
492 \textcolor{keywordflow}{        endif}
493         dte\_term = dte\_t2 + hp\_b(k) * (t0(k)-t0(k-1))
494         dse\_term = dse\_t2 + hp\_b(k) * (s0(k)-s0(k-1))
495       \textcolor{keywordflow}{else}
496         th\_a(k-1) = h\_tr(k-1) * t0(k-1) ; sh\_a(k-1) = h\_tr(k-1) * s0(k-1)
497         \textcolor{keywordflow}{if} (k>=nz) \textcolor{keywordflow}{then}
498           th\_b(k) = h\_tr(k) * t0(k) ; sh\_b(k) = h\_tr(k) * s0(k)
499         \textcolor{keywordflow}{else}
500           th\_b(k) = h\_tr(k) * t0(k) + kddt\_h(k+1) * te(k+1)
501           sh\_b(k) = h\_tr(k) * s0(k) + kddt\_h(k+1) * se(k+1)
502 \textcolor{keywordflow}{        endif}
503 \textcolor{keywordflow}{      endif}
504 
505       \textcolor{comment}{! Find the energy change due to a guess at the strength of diffusion at interface K.}
506       kddt\_h\_guess = kddt\_h(k)
507 
508       \textcolor{keywordflow}{if} (old\_pe\_calc) \textcolor{keywordflow}{then}
509         \textcolor{keyword}{call }find\_pe\_chg\_orig(kddt\_h\_guess, h\_tr(k-1), hp\_b(k), &
510                          dte\_term, dse\_term, dt\_k\_t2, ds\_k\_t2, &
511                          dt\_to\_dpe(k-1), ds\_to\_dpe(k-1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), &
512                          pres\_z(k), dt\_to\_dcolht(k-1), ds\_to\_dcolht(k-1), &
513                          dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), &
514                          pe\_chg=pe\_chg\_k(k,2), dpec\_dkd=dpeb\_dkd(k))
515       \textcolor{keywordflow}{else}
516         \textcolor{keyword}{call }find\_pe\_chg(0.0, kddt\_h\_guess, hp\_a(k-1), hp\_b(k), &
517                          th\_a(k-1), sh\_a(k-1), th\_b(k), sh\_b(k), &
518                          dt\_to\_dpe\_a(k-1), ds\_to\_dpe\_a(k-1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), &
519                          pres\_z(k), dt\_to\_dcolht\_a(k-1), ds\_to\_dcolht\_a(k-1), &
520                          dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), &
521                          pe\_chg=pe\_chg\_k(k,2), dpec\_dkd=dpeb\_dkd(k), &
522                          colht\_cor=colht\_cor\_k(k,2))
523 \textcolor{keywordflow}{      endif}
524 
525       \textcolor{keywordflow}{if} (debug) \textcolor{keywordflow}{then}
526         \textcolor{comment}{! Compare with a 4th-order finite difference estimate.}
527         \textcolor{keywordflow}{do} itt=1,5
528           kddt\_h\_guess = (1.0+0.01*(itt-3))*kddt\_h(k)
529 
530           \textcolor{keywordflow}{if} (old\_pe\_calc) \textcolor{keywordflow}{then}
531             \textcolor{keyword}{call }find\_pe\_chg\_orig(kddt\_h\_guess, h\_tr(k-1), hp\_b(k), &
532                            dte\_term, dse\_term, dt\_k\_t2, ds\_k\_t2, &
533                            dt\_to\_dpe(k-1), ds\_to\_dpe(k-1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), &
534                            pres\_z(k), dt\_to\_dcolht(k-1), ds\_to\_dcolht(k-1), &
535                            dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), &
536                            pe\_chg=pe\_chg(itt))
537           \textcolor{keywordflow}{else}
538             \textcolor{keyword}{call }find\_pe\_chg(0.0, kddt\_h\_guess, hp\_a(k-1), hp\_b(k), &
539                              th\_a(k-1), sh\_a(k-1), th\_b(k), sh\_b(k), &
540                              dt\_to\_dpe\_a(k-1), ds\_to\_dpe\_a(k-1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), &
541                              pres\_z(k), dt\_to\_dcolht\_a(k-1), ds\_to\_dcolht\_a(k-1), &
542                              dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), &
543                              pe\_chg=pe\_chg(itt))
544 \textcolor{keywordflow}{          endif}
545 \textcolor{keywordflow}{        enddo}
546 
547         dpeb\_dkd\_est(k) = (4.0*(pe\_chg(4)-pe\_chg(2))/(0.02*kddt\_h(k)) - &
548                                (pe\_chg(5)-pe\_chg(1))/(0.04*kddt\_h(k))) / 3.0
549         dpeb\_dkd\_trunc(k) = (pe\_chg(4)-pe\_chg(2))/(0.02*kddt\_h(k)) - &
550                             (pe\_chg(5)-pe\_chg(1))/(0.04*kddt\_h(k))
551         dpeb\_dkd\_err(k) = (dpeb\_dkd\_est(k) - dpeb\_dkd(k))
552         dpeb\_dkd\_err\_norm(k) = (dpeb\_dkd\_est(k) - dpeb\_dkd(k)) / &
553                               (abs(dpeb\_dkd\_est(k)) + abs(dpeb\_dkd(k)) + 1e-100*us%RZ\_to\_kg\_m2*us
      %L\_T\_to\_m\_s**2)
554 \textcolor{keywordflow}{      endif}
555 
556       \textcolor{comment}{!   At this point, the final value of Kddt\_h(K) is known, so the estimated}
557       \textcolor{comment}{! properties for layer k can be calculated.}
558 
559       b1 = 1.0 / (hp\_b(k) + kddt\_h(k))
560       c1\_b(k) = kddt\_h(k) * b1
561       \textcolor{keywordflow}{if} (k==nz) \textcolor{keywordflow}{then}
562         te(nz) = b1* (h\_tr(nz)*t0(nz))
563         se(nz) = b1* (h\_tr(nz)*s0(nz))
564       \textcolor{keywordflow}{else}
565         te(k) = b1 * (h\_tr(k) * t0(k) + kddt\_h(k+1) * te(k+1))
566         se(k) = b1 * (h\_tr(k) * s0(k) + kddt\_h(k+1) * se(k+1))
567 \textcolor{keywordflow}{      endif}
568       \textcolor{keywordflow}{if} (old\_pe\_calc) \textcolor{keywordflow}{then}
569         dte(k) = b1 * ( kddt\_h(k)*(t0(k-1)-t0(k)) + dte\_t2 )
570         dse(k) = b1 * ( kddt\_h(k)*(s0(k-1)-s0(k)) + dse\_t2 )
571 \textcolor{keywordflow}{      endif}
572 
573       hp\_b(k-1) = h\_tr(k-1) + (hp\_b(k) * b1) * kddt\_h(k)
574       dt\_to\_dpe\_b(k-1) = dt\_to\_dpe(k-1) + c1\_b(k)*dt\_to\_dpe\_b(k)
575       ds\_to\_dpe\_b(k-1) = ds\_to\_dpe(k-1) + c1\_b(k)*ds\_to\_dpe\_b(k)
576       dt\_to\_dcolht\_b(k-1) = dt\_to\_dcolht(k-1) + c1\_b(k)*dt\_to\_dcolht\_b(k)
577       ds\_to\_dcolht\_b(k-1) = ds\_to\_dcolht(k-1) + c1\_b(k)*ds\_to\_dcolht\_b(k)
578 
579 \textcolor{keywordflow}{    enddo}
580 
581     b1 = 1.0 / (hp\_b(1))
582     tf(1) = b1 * (h\_tr(1) * t0(1) + kddt\_h(2) * te(2))
583     sf(1) = b1 * (h\_tr(1) * s0(1) + kddt\_h(2) * se(2))
584     \textcolor{keywordflow}{if} (old\_pe\_calc) \textcolor{keywordflow}{then}
585       dte(1) = b1 * kddt\_h(2) * ((t0(2)-t0(1)) + dte(2))
586       dse(1) = b1 * kddt\_h(2) * ((s0(2)-s0(1)) + dse(2))
587 \textcolor{keywordflow}{    endif}
588 
589     \textcolor{keywordflow}{do} k=2,nz
590       tf(k) = te(k) + c1\_b(k)*tf(k-1)
591       sf(k) = se(k) + c1\_b(k)*sf(k-1)
592 \textcolor{keywordflow}{    enddo}
593 
594     \textcolor{keywordflow}{if} (debug) \textcolor{keywordflow}{then}
595       \textcolor{keywordflow}{do} k=1,nz ; tb(k) = tf(k) ; sb(k) = sf(k) ;\textcolor{keywordflow}{ enddo}
596       pe\_chg\_tot1b = 0.0 ; pe\_chg\_tot2b = 0.0 ; t\_chg\_totb = 0.0
597       \textcolor{keywordflow}{do} k=1,nz
598         pe\_chg\_tot1b = pe\_chg\_tot1b + (dt\_to\_dpe(k) * (tf(k) - t0(k)) + &
599                                        ds\_to\_dpe(k) * (sf(k) - s0(k)))
600         t\_chg\_totb = t\_chg\_totb + h\_tr(k) * (tf(k) - t0(k))
601 \textcolor{keywordflow}{      enddo}
602       \textcolor{keywordflow}{do} k=2,nz
603         pe\_chg\_tot2b = pe\_chg\_tot2b + (pe\_chg\_k(k,2) - colht\_cor\_k(k,2))
604 \textcolor{keywordflow}{      enddo}
605 \textcolor{keywordflow}{    endif}
606 
607 \textcolor{keywordflow}{  endif}
608 
609   \textcolor{keywordflow}{if} (central) \textcolor{keywordflow}{then}
610 
611     \textcolor{comment}{! Set up values appropriate for no diffusivity.}
612     \textcolor{keywordflow}{do} k=1,nz
613       hp\_a(k) = h\_tr(k) ; hp\_b(k) = h\_tr(k)
614       dt\_to\_dpe\_a(k) = dt\_to\_dpe(k) ; ds\_to\_dpe\_a(k) = ds\_to\_dpe(k)
615       dt\_to\_dpe\_b(k) = dt\_to\_dpe(k) ; ds\_to\_dpe\_b(k) = ds\_to\_dpe(k)
616       dt\_to\_dcolht\_a(k) = dt\_to\_dcolht(k) ; ds\_to\_dcolht\_a(k) = ds\_to\_dcolht(k)
617       dt\_to\_dcolht\_b(k) = dt\_to\_dcolht(k) ; ds\_to\_dcolht\_b(k) = ds\_to\_dcolht(k)
618 \textcolor{keywordflow}{    enddo}
619 
620     \textcolor{comment}{! Calculate the dependencies on layers above.}
621     kddt\_h\_a(1) = 0.0
622     \textcolor{keywordflow}{do} k=2,nz  \textcolor{comment}{! Loop over interior interfaces.}
623       \textcolor{comment}{! First calculate some terms that are independent of the change in Kddt\_h(K).}
624       kd0 = 0.0  \textcolor{comment}{! This might need to be changed - it is the already applied value of Kddt\_h(K).}
625       \textcolor{keywordflow}{if} (k<=2) \textcolor{keywordflow}{then}
626         th\_a(k-1) = h\_tr(k-1) * t0(k-1) ; sh\_a(k-1) = h\_tr(k-1) * s0(k-1)
627       \textcolor{keywordflow}{else}
628         th\_a(k-1) = h\_tr(k-1) * t0(k-1) + kddt\_h(k-1) * te\_a(k-2)
629         sh\_a(k-1) = h\_tr(k-1) * s0(k-1) + kddt\_h(k-1) * se\_a(k-2)
630 \textcolor{keywordflow}{      endif}
631       th\_b(k) = h\_tr(k) * t0(k) ; sh\_b(k) = h\_tr(k) * s0(k)
632 
633       kddt\_h\_a(k) = 0.0 ; \textcolor{keywordflow}{if} (k < k\_cent) kddt\_h\_a(k) = kddt\_h(k)
634       dkd = kddt\_h\_a(k)
635 
636       \textcolor{keyword}{call }find\_pe\_chg(kd0, dkd, hp\_a(k-1), hp\_b(k), &
637                        th\_a(k-1), sh\_a(k-1), th\_b(k), sh\_b(k), &
638                        dt\_to\_dpe\_a(k-1), ds\_to\_dpe\_a(k-1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), &
639                        pres\_z(k), dt\_to\_dcolht\_a(k-1), ds\_to\_dcolht\_a(k-1), &
640                        dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), &
641                        pe\_chg=pe\_change, colht\_cor=colht\_cor)
642       pe\_chg\_k(k,3) = pe\_change
643       colht\_cor\_k(k,3) = colht\_cor
644 
645       b1 = 1.0 / (hp\_a(k-1) + kddt\_h\_a(k))
646       c1\_a(k) = kddt\_h\_a(k) * b1
647       \textcolor{keywordflow}{if} (k==2) \textcolor{keywordflow}{then}
648         te\_a(1) = b1*(h\_tr(1)*t0(1))
649         se\_a(1) = b1*(h\_tr(1)*s0(1))
650       \textcolor{keywordflow}{else}
651         te\_a(k-1) = b1 * (h\_tr(k-1) * t0(k-1) + kddt\_h\_a(k-1) * te\_a(k-2))
652         se\_a(k-1) = b1 * (h\_tr(k-1) * s0(k-1) + kddt\_h\_a(k-1) * se\_a(k-2))
653 \textcolor{keywordflow}{      endif}
654 
655       hp\_a(k) = h\_tr(k) + (hp\_a(k-1) * b1) * kddt\_h\_a(k)
656       dt\_to\_dpe\_a(k) = dt\_to\_dpe(k) + c1\_a(k)*dt\_to\_dpe\_a(k-1)
657       ds\_to\_dpe\_a(k) = ds\_to\_dpe(k) + c1\_a(k)*ds\_to\_dpe\_a(k-1)
658       dt\_to\_dcolht\_a(k) = dt\_to\_dcolht(k) + c1\_a(k)*dt\_to\_dcolht\_a(k-1)
659       ds\_to\_dcolht\_a(k) = ds\_to\_dcolht(k) + c1\_a(k)*ds\_to\_dcolht\_a(k-1)
660 \textcolor{keywordflow}{    enddo}
661 
662     \textcolor{comment}{! Calculate the dependencies on layers below.}
663     kddt\_h\_b(nz+1) = 0.0
664     \textcolor{keywordflow}{do} k=nz,2,-1  \textcolor{comment}{! Loop over interior interfaces.}
665       \textcolor{comment}{! First calculate some terms that are independent of the change in Kddt\_h(K).}
666       kd0 = 0.0  \textcolor{comment}{! This might need to be changed - it is the already applied value of Kddt\_h(K).}
667 \textcolor{comment}{!     if (K<=2) then}
668         th\_a(k-1) = h\_tr(k-1) * t0(k-1) ; sh\_a(k-1) = h\_tr(k-1) * s0(k-1)
669 \textcolor{comment}{!     else}
670 \textcolor{comment}{!       Th\_a(k-1) = h\_tr(k-1) * T0(k-1) + Kddt\_h(K-1) * Te\_a(k-2)}
671 \textcolor{comment}{!       Sh\_a(k-1) = h\_tr(k-1) * S0(k-1) + Kddt\_h(K-1) * Se\_a(k-2)}
672 \textcolor{comment}{!     endif}
673       \textcolor{keywordflow}{if} (k>=nz) \textcolor{keywordflow}{then}
674         th\_b(k) = h\_tr(k) * t0(k) ; sh\_b(k) = h\_tr(k) * s0(k)
675       \textcolor{keywordflow}{else}
676         th\_b(k) = h\_tr(k) * t0(k) + kddt\_h(k+1) * te\_b(k+1)
677         sh\_b(k) = h\_tr(k) * s0(k) + kddt\_h(k+1) * se\_b(k+1)
678 \textcolor{keywordflow}{      endif}
679 
680       kddt\_h\_b(k) = 0.0 ; \textcolor{keywordflow}{if} (k > k\_cent) kddt\_h\_b(k) = kddt\_h(k)
681       dkd = kddt\_h\_b(k)
682 
683       \textcolor{keyword}{call }find\_pe\_chg(kd0, dkd, hp\_a(k-1), hp\_b(k), &
684                        th\_a(k-1), sh\_a(k-1), th\_b(k), sh\_b(k), &
685                        dt\_to\_dpe\_a(k-1), ds\_to\_dpe\_a(k-1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), &
686                        pres\_z(k), dt\_to\_dcolht\_a(k-1), ds\_to\_dcolht\_a(k-1), &
687                        dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), &
688                        pe\_chg=pe\_change, colht\_cor=colht\_cor)
689       pe\_chg\_k(k,3) = pe\_chg\_k(k,3) + pe\_change
690       colht\_cor\_k(k,3) = colht\_cor\_k(k,3) + colht\_cor
691 
692       b1 = 1.0 / (hp\_b(k) + kddt\_h\_b(k))
693       c1\_b(k) = kddt\_h\_b(k) * b1
694       \textcolor{keywordflow}{if} (k==nz) \textcolor{keywordflow}{then}
695         te\_b(k) = b1 * (h\_tr(k)*t0(k))
696         se\_b(k) = b1 * (h\_tr(k)*s0(k))
697       \textcolor{keywordflow}{else}
698         te\_b(k) = b1 * (h\_tr(k) * t0(k) + kddt\_h\_b(k+1) * te\_b(k+1))
699         se\_b(k) = b1 * (h\_tr(k) * s0(k) + kddt\_h\_b(k+1) * se\_b(k+1))
700 \textcolor{keywordflow}{      endif}
701 
702       hp\_b(k-1) = h\_tr(k-1) + (hp\_b(k) * b1) * kddt\_h\_b(k)
703       dt\_to\_dpe\_b(k-1) = dt\_to\_dpe(k-1) + c1\_b(k)*dt\_to\_dpe\_b(k)
704       ds\_to\_dpe\_b(k-1) = ds\_to\_dpe(k-1) + c1\_b(k)*ds\_to\_dpe\_b(k)
705       dt\_to\_dcolht\_b(k-1) = dt\_to\_dcolht(k-1) + c1\_b(k)*dt\_to\_dcolht\_b(k)
706       ds\_to\_dcolht\_b(k-1) = ds\_to\_dcolht(k-1) + c1\_b(k)*ds\_to\_dcolht\_b(k)
707 
708 \textcolor{keywordflow}{    enddo}
709 
710     \textcolor{comment}{! Calculate the final solution for the layers surrounding interface K\_cent}
711     k = k\_cent
712 
713     \textcolor{comment}{! First calculate some terms that are independent of the change in Kddt\_h(K).}
714     kd0 = 0.0  \textcolor{comment}{! This might need to be changed - it is the already applied value of Kddt\_h(K).}
715     \textcolor{keywordflow}{if} (k<=2) \textcolor{keywordflow}{then}
716       th\_a(k-1) = h\_tr(k-1) * t0(k-1) ; sh\_a(k-1) = h\_tr(k-1) * s0(k-1)
717     \textcolor{keywordflow}{else}
718       th\_a(k-1) = h\_tr(k-1) * t0(k-1) + kddt\_h(k-1) * te\_a(k-2)
719       sh\_a(k-1) = h\_tr(k-1) * s0(k-1) + kddt\_h(k-1) * se\_a(k-2)
720 \textcolor{keywordflow}{    endif}
721     \textcolor{keywordflow}{if} (k>=nz) \textcolor{keywordflow}{then}
722       th\_b(k) = h\_tr(k) * t0(k) ; sh\_b(k) = h\_tr(k) * s0(k)
723     \textcolor{keywordflow}{else}
724       th\_b(k) = h\_tr(k) * t0(k) + kddt\_h(k+1) * te\_b(k+1)
725       sh\_b(k) = h\_tr(k) * s0(k) + kddt\_h(k+1) * se\_b(k+1)
726 \textcolor{keywordflow}{    endif}
727 
728     dkd = kddt\_h(k)
729 
730     \textcolor{keyword}{call }find\_pe\_chg(kd0, dkd, hp\_a(k-1), hp\_b(k), &
731                      th\_a(k-1), sh\_a(k-1), th\_b(k), sh\_b(k), &
732                      dt\_to\_dpe\_a(k-1), ds\_to\_dpe\_a(k-1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), &
733                      pres\_z(k), dt\_to\_dcolht\_a(k-1), ds\_to\_dcolht\_a(k-1), &
734                      dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), &
735                      pe\_chg=pe\_change, colht\_cor=colht\_cor)
736     pe\_chg\_k(k,3) = pe\_chg\_k(k,3) + pe\_change
737     colht\_cor\_k(k,3) = colht\_cor\_k(k,3) + colht\_cor
738 
739 
740     \textcolor{comment}{!   To derive the following, first to a partial update for the estimated}
741     \textcolor{comment}{! temperatures and salinities in the layers around this interface:}
742     \textcolor{comment}{!   b1\_a = 1.0 / (hp\_a(k-1) + Kddt\_h(K))}
743     \textcolor{comment}{!   b1\_b = 1.0 / (hp\_b(k) + Kddt\_h(K))}
744     \textcolor{comment}{!   Te\_up(k) = Th\_b(k) * b1\_b ; Se\_up(k) = Sh\_b(k) * b1\_b}
745     \textcolor{comment}{!   Te\_up(k-1) = Th\_a(k-1) * b1\_a ; Se\_up(k-1) = Sh\_a(k-1) * b1\_a}
746     \textcolor{comment}{! Find the final values of T & S for both layers around K\_cent, using that}
747     \textcolor{comment}{!   c1\_a(K) = Kddt\_h(K) * b1\_a ; c1\_b(K) = Kddt\_h(K) * b1\_b}
748     \textcolor{comment}{!   Tf(K\_cent-1) = Te\_up(K\_cent-1) + c1\_a(K\_cent)*Tf(K\_cent)}
749     \textcolor{comment}{!   Tf(K\_cent)   = Te\_up(K\_cent) + c1\_b(K\_cent)*Tf(K\_cent-1)}
750     \textcolor{comment}{! but further exploiting the expressions for c1\_a and c1\_b to avoid}
751     \textcolor{comment}{! subtraction in the denominator, and use only a single division.}
752     b1 = 1.0 / (hp\_a(k-1)*hp\_b(k) + kddt\_h(k)*(hp\_a(k-1) + hp\_b(k)))
753     tf(k-1) = ((hp\_b(k) + kddt\_h(k)) * th\_a(k-1) + kddt\_h(k) * th\_b(k) ) * b1
754     sf(k-1) = ((hp\_b(k) + kddt\_h(k)) * sh\_a(k-1) + kddt\_h(k) * sh\_b(k) ) * b1
755     tf(k) = (kddt\_h(k) * th\_a(k-1) + (hp\_a(k-1) + kddt\_h(k)) * th\_b(k) ) * b1
756     sf(k) = (kddt\_h(k) * sh\_a(k-1) + (hp\_a(k-1) + kddt\_h(k)) * sh\_b(k) ) * b1
757 
758     c1\_a(k) = kddt\_h(k) / (hp\_a(k-1) + kddt\_h(k))
759     c1\_b(k) = kddt\_h(k) / (hp\_b(k) + kddt\_h(k))
760 
761     \textcolor{comment}{! Now update the other layer working outward from k\_cent to determine the final}
762     \textcolor{comment}{! temperatures and salinities.}
763     \textcolor{keywordflow}{do} k=k\_cent-2,1,-1
764       tf(k) = te\_a(k) + c1\_a(k+1)*tf(k+1)
765       sf(k) = se\_a(k) + c1\_a(k+1)*sf(k+1)
766 \textcolor{keywordflow}{    enddo}
767     \textcolor{keywordflow}{do} k=k\_cent+1,nz
768       tf(k) = te\_b(k) + c1\_b(k)*tf(k-1)
769       sf(k) = se\_b(k) + c1\_b(k)*sf(k-1)
770 \textcolor{keywordflow}{    enddo}
771 
772     \textcolor{keywordflow}{if} (debug) \textcolor{keywordflow}{then}
773       pe\_chg\_tot1c = 0.0 ; pe\_chg\_tot2c = 0.0 ; t\_chg\_totc = 0.0
774       \textcolor{keywordflow}{do} k=1,nz
775         pe\_chg\_tot1c = pe\_chg\_tot1c + (dt\_to\_dpe(k) * (tf(k) - t0(k)) + &
776                                        ds\_to\_dpe(k) * (sf(k) - s0(k)))
777         t\_chg\_totc = t\_chg\_totc + h\_tr(k) * (tf(k) - t0(k))
778 \textcolor{keywordflow}{      enddo}
779       \textcolor{keywordflow}{do} k=2,nz
780         pe\_chg\_tot2c = pe\_chg\_tot2c + (pe\_chg\_k(k,3) - colht\_cor\_k(k,3))
781 \textcolor{keywordflow}{      enddo}
782 \textcolor{keywordflow}{    endif}
783 
784 \textcolor{keywordflow}{  endif}
785 
786   \textcolor{keywordflow}{if} (halves) \textcolor{keywordflow}{then}
787 
788     \textcolor{comment}{! Set up values appropriate for no diffusivity.}
789     \textcolor{keywordflow}{do} k=1,nz
790       hp\_a(k) = h\_tr(k) ; hp\_b(k) = h\_tr(k)
791       dt\_to\_dpe\_a(k) = dt\_to\_dpe(k) ; ds\_to\_dpe\_a(k) = ds\_to\_dpe(k)
792       dt\_to\_dpe\_b(k) = dt\_to\_dpe(k) ; ds\_to\_dpe\_b(k) = ds\_to\_dpe(k)
793       dt\_to\_dcolht\_a(k) = dt\_to\_dcolht(k) ; ds\_to\_dcolht\_a(k) = ds\_to\_dcolht(k)
794       dt\_to\_dcolht\_b(k) = dt\_to\_dcolht(k) ; ds\_to\_dcolht\_b(k) = ds\_to\_dcolht(k)
795 \textcolor{keywordflow}{    enddo}
796     \textcolor{keywordflow}{do} k=1,nz+1
797       kd\_so\_far(k) = 0.0
798 \textcolor{keywordflow}{    enddo}
799 
800     \textcolor{comment}{! Calculate the dependencies on layers above.}
801     kddt\_h\_a(1) = 0.0
802     \textcolor{keywordflow}{do} k=2,nz  \textcolor{comment}{! Loop over interior interfaces.}
803       \textcolor{comment}{! First calculate some terms that are independent of the change in Kddt\_h(K).}
804       kd0 = kd\_so\_far(k)
805       \textcolor{keywordflow}{if} (k<=2) \textcolor{keywordflow}{then}
806         th\_a(k-1) = h\_tr(k-1) * t0(k-1) ; sh\_a(k-1) = h\_tr(k-1) * s0(k-1)
807       \textcolor{keywordflow}{else}
808         th\_a(k-1) = h\_tr(k-1) * t0(k-1) + kd\_so\_far(k-1) * te(k-2)
809         sh\_a(k-1) = h\_tr(k-1) * s0(k-1) + kd\_so\_far(k-1) * se(k-2)
810 \textcolor{keywordflow}{      endif}
811       th\_b(k) = h\_tr(k) * t0(k) ; sh\_b(k) = h\_tr(k) * s0(k)
812 
813       dkd = 0.5 * kddt\_h(k) - kd\_so\_far(k)
814 
815       \textcolor{keyword}{call }find\_pe\_chg(kd0, dkd, hp\_a(k-1), hp\_b(k), &
816                        th\_a(k-1), sh\_a(k-1), th\_b(k), sh\_b(k), &
817                        dt\_to\_dpe\_a(k-1), ds\_to\_dpe\_a(k-1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), &
818                        pres\_z(k), dt\_to\_dcolht\_a(k-1), ds\_to\_dcolht\_a(k-1), &
819                        dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), &
820                        pe\_chg=pe\_change, colht\_cor=colht\_cor)
821 
822       pe\_chg\_k(k,4) = pe\_change
823       colht\_cor\_k(k,4) = colht\_cor
824 
825       kd\_so\_far(k) = kd\_so\_far(k) + dkd
826 
827       b1 = 1.0 / (hp\_a(k-1) + kd\_so\_far(k))
828       c1\_a(k) = kd\_so\_far(k) * b1
829       \textcolor{keywordflow}{if} (k==2) \textcolor{keywordflow}{then}
830         te(1) = b1*(h\_tr(1)*t0(1))
831         se(1) = b1*(h\_tr(1)*s0(1))
832       \textcolor{keywordflow}{else}
833         te(k-1) = b1 * (h\_tr(k-1) * t0(k-1) + kd\_so\_far(k-1) * te(k-2))
834         se(k-1) = b1 * (h\_tr(k-1) * s0(k-1) + kd\_so\_far(k-1) * se(k-2))
835 \textcolor{keywordflow}{      endif}
836 
837       hp\_a(k) = h\_tr(k) + (hp\_a(k-1) * b1) * kd\_so\_far(k)
838       dt\_to\_dpe\_a(k) = dt\_to\_dpe(k) + c1\_a(k)*dt\_to\_dpe\_a(k-1)
839       ds\_to\_dpe\_a(k) = ds\_to\_dpe(k) + c1\_a(k)*ds\_to\_dpe\_a(k-1)
840       dt\_to\_dcolht\_a(k) = dt\_to\_dcolht(k) + c1\_a(k)*dt\_to\_dcolht\_a(k-1)
841       ds\_to\_dcolht\_a(k) = ds\_to\_dcolht(k) + c1\_a(k)*ds\_to\_dcolht\_a(k-1)
842 \textcolor{keywordflow}{    enddo}
843 
844     \textcolor{comment}{! Calculate the dependencies on layers below.}
845     \textcolor{keywordflow}{do} k=nz,2,-1  \textcolor{comment}{! Loop over interior interfaces.}
846       \textcolor{comment}{! First calculate some terms that are independent of the change in Kddt\_h(K).}
847       kd0 = kd\_so\_far(k)
848       \textcolor{keywordflow}{if} (k<=2) \textcolor{keywordflow}{then}
849         th\_a(k-1) = h\_tr(k-1) * t0(k-1) ; sh\_a(k-1) = h\_tr(k-1) * s0(k-1)
850       \textcolor{keywordflow}{else}
851         th\_a(k-1) = h\_tr(k-1) * t0(k-1) + kd\_so\_far(k-1) * te(k-2)
852         sh\_a(k-1) = h\_tr(k-1) * s0(k-1) + kd\_so\_far(k-1) * se(k-2)
853 \textcolor{keywordflow}{      endif}
854       \textcolor{keywordflow}{if} (k>=nz) \textcolor{keywordflow}{then}
855         th\_b(k) = h\_tr(k) * t0(k) ; sh\_b(k) = h\_tr(k) * s0(k)
856       \textcolor{keywordflow}{else}
857         th\_b(k) = h\_tr(k) * t0(k) + kd\_so\_far(k+1) * te(k+1)
858         sh\_b(k) = h\_tr(k) * s0(k) + kd\_so\_far(k+1) * se(k+1)
859 \textcolor{keywordflow}{      endif}
860 
861       dkd = kddt\_h(k) - kd\_so\_far(k)
862 
863       \textcolor{keyword}{call }find\_pe\_chg(kd0, dkd, hp\_a(k-1), hp\_b(k), &
864                        th\_a(k-1), sh\_a(k-1), th\_b(k), sh\_b(k), &
865                        dt\_to\_dpe\_a(k-1), ds\_to\_dpe\_a(k-1), dt\_to\_dpe\_b(k), ds\_to\_dpe\_b(k), &
866                        pres\_z(k), dt\_to\_dcolht\_a(k-1), ds\_to\_dcolht\_a(k-1), &
867                        dt\_to\_dcolht\_b(k), ds\_to\_dcolht\_b(k), &
868                        pe\_chg=pe\_change, colht\_cor=colht\_cor)
869 
870       pe\_chg\_k(k,4) = pe\_chg\_k(k,4) + pe\_change
871       colht\_cor\_k(k,4) = colht\_cor\_k(k,4) + colht\_cor
872 
873 
874       kd\_so\_far(k) = kd\_so\_far(k) + dkd
875 
876       b1 = 1.0 / (hp\_b(k) + kd\_so\_far(k))
877       c1\_b(k) = kd\_so\_far(k) * b1
878       \textcolor{keywordflow}{if} (k==nz) \textcolor{keywordflow}{then}
879         te(k) = b1 * (h\_tr(k)*t0(k))
880         se(k) = b1 * (h\_tr(k)*s0(k))
881       \textcolor{keywordflow}{else}
882         te(k) = b1 * (h\_tr(k) * t0(k) + kd\_so\_far(k+1) * te(k+1))
883         se(k) = b1 * (h\_tr(k) * s0(k) + kd\_so\_far(k+1) * se(k+1))
884 \textcolor{keywordflow}{      endif}
885 
886       hp\_b(k-1) = h\_tr(k-1) + (hp\_b(k) * b1) * kd\_so\_far(k)
887       dt\_to\_dpe\_b(k-1) = dt\_to\_dpe(k-1) + c1\_b(k)*dt\_to\_dpe\_b(k)
888       ds\_to\_dpe\_b(k-1) = ds\_to\_dpe(k-1) + c1\_b(k)*ds\_to\_dpe\_b(k)
889       dt\_to\_dcolht\_b(k-1) = dt\_to\_dcolht(k-1) + c1\_b(k)*dt\_to\_dcolht\_b(k)
890       ds\_to\_dcolht\_b(k-1) = ds\_to\_dcolht(k-1) + c1\_b(k)*ds\_to\_dcolht\_b(k)
891 
892 \textcolor{keywordflow}{    enddo}
893 
894     \textcolor{comment}{! Now update the other layer working down from the top to determine the}
895     \textcolor{comment}{! final temperatures and salinities.}
896     b1 = 1.0 / (hp\_b(1))
897     tf(1) = b1 * (h\_tr(1) * t0(1) + kddt\_h(2) * te(2))
898     sf(1) = b1 * (h\_tr(1) * s0(1) + kddt\_h(2) * se(2))
899     \textcolor{keywordflow}{do} k=2,nz
900       tf(k) = te(k) + c1\_b(k)*tf(k-1)
901       sf(k) = se(k) + c1\_b(k)*sf(k-1)
902 \textcolor{keywordflow}{    enddo}
903 
904     \textcolor{keywordflow}{if} (debug) \textcolor{keywordflow}{then}
905       pe\_chg\_tot1d = 0.0 ; pe\_chg\_tot2d = 0.0 ; t\_chg\_totd = 0.0
906       \textcolor{keywordflow}{do} k=1,nz
907         pe\_chg\_tot1d = pe\_chg\_tot1d + (dt\_to\_dpe(k) * (tf(k) - t0(k)) + &
908                                        ds\_to\_dpe(k) * (sf(k) - s0(k)))
909         t\_chg\_totd = t\_chg\_totd + h\_tr(k) * (tf(k) - t0(k))
910 \textcolor{keywordflow}{      enddo}
911       \textcolor{keywordflow}{do} k=2,nz
912         pe\_chg\_tot2d = pe\_chg\_tot2d + (pe\_chg\_k(k,4) - colht\_cor\_k(k,4))
913 \textcolor{keywordflow}{      enddo}
914 \textcolor{keywordflow}{    endif}
915 
916 \textcolor{keywordflow}{  endif}
917 
918   energy\_kd = 0.0 ; \textcolor{keywordflow}{do} k=2,nz ; energy\_kd = energy\_kd + pe\_chg\_k(k,1) ;\textcolor{keywordflow}{ enddo}
919   energy\_kd = energy\_kd / dt
920 
921   \textcolor{keywordflow}{if} (do\_print) \textcolor{keywordflow}{then}
922     \textcolor{keywordflow}{if} (cs%id\_ERt>0) \textcolor{keyword}{call }post\_data(cs%id\_ERt, pe\_chg\_k(:,1), cs%diag)
923     \textcolor{keywordflow}{if} (cs%id\_ERb>0) \textcolor{keyword}{call }post\_data(cs%id\_ERb, pe\_chg\_k(:,2), cs%diag)
924     \textcolor{keywordflow}{if} (cs%id\_ERc>0) \textcolor{keyword}{call }post\_data(cs%id\_ERc, pe\_chg\_k(:,3), cs%diag)
925     \textcolor{keywordflow}{if} (cs%id\_ERh>0) \textcolor{keyword}{call }post\_data(cs%id\_ERh, pe\_chg\_k(:,4), cs%diag)
926     \textcolor{keywordflow}{if} (cs%id\_Kddt>0) \textcolor{keyword}{call }post\_data(cs%id\_Kddt, kddt\_h, cs%diag)
927     \textcolor{keywordflow}{if} (cs%id\_Kd>0)   \textcolor{keyword}{call }post\_data(cs%id\_Kd,   kd, cs%diag)
928     \textcolor{keywordflow}{if} (cs%id\_h>0)    \textcolor{keyword}{call }post\_data(cs%id\_h,    h\_tr, cs%diag)
929     \textcolor{keywordflow}{if} (cs%id\_zInt>0) \textcolor{keyword}{call }post\_data(cs%id\_zInt, z\_int, cs%diag)
930     \textcolor{keywordflow}{if} (cs%id\_CHCt>0) \textcolor{keyword}{call }post\_data(cs%id\_CHCt, colht\_cor\_k(:,1), cs%diag)
931     \textcolor{keywordflow}{if} (cs%id\_CHCb>0) \textcolor{keyword}{call }post\_data(cs%id\_CHCb, colht\_cor\_k(:,2), cs%diag)
932     \textcolor{keywordflow}{if} (cs%id\_CHCc>0) \textcolor{keyword}{call }post\_data(cs%id\_CHCc, colht\_cor\_k(:,3), cs%diag)
933     \textcolor{keywordflow}{if} (cs%id\_CHCh>0) \textcolor{keyword}{call }post\_data(cs%id\_CHCh, colht\_cor\_k(:,4), cs%diag)
934     \textcolor{keywordflow}{if} (cs%id\_T0>0) \textcolor{keyword}{call }post\_data(cs%id\_T0, t0, cs%diag)
935     \textcolor{keywordflow}{if} (cs%id\_Tf>0) \textcolor{keyword}{call }post\_data(cs%id\_Tf, tf, cs%diag)
936     \textcolor{keywordflow}{if} (cs%id\_S0>0) \textcolor{keyword}{call }post\_data(cs%id\_S0, s0, cs%diag)
937     \textcolor{keywordflow}{if} (cs%id\_Sf>0) \textcolor{keyword}{call }post\_data(cs%id\_Sf, sf, cs%diag)
938     \textcolor{keywordflow}{if} (cs%id\_N2\_0>0) \textcolor{keywordflow}{then}
939       n2(1) = 0.0 ; n2(nz+1) = 0.0
940       \textcolor{keywordflow}{do} k=2,nz
941         \textcolor{keyword}{call }calculate\_density(0.5*(t0(k-1) + t0(k)), 0.5*(s0(k-1) + s0(k)), &
942                                pres(k), rho\_here, tv%eqn\_of\_state)
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)))) * &
944                 ( 0.5*(dsv\_dt(k-1) + dsv\_dt(k)) * (t0(k-1) - t0(k)) + &
945                   0.5*(dsv\_ds(k-1) + dsv\_ds(k)) * (s0(k-1) - s0(k)) )
946 \textcolor{keywordflow}{      enddo}
947       \textcolor{keyword}{call }post\_data(cs%id\_N2\_0, n2, cs%diag)
948 \textcolor{keywordflow}{    endif}
949     \textcolor{keywordflow}{if} (cs%id\_N2\_f>0) \textcolor{keywordflow}{then}
950       n2(1) = 0.0 ; n2(nz+1) = 0.0
951       \textcolor{keywordflow}{do} k=2,nz
952         \textcolor{keyword}{call }calculate\_density(0.5*(tf(k-1) + tf(k)), 0.5*(sf(k-1) + sf(k)), &
953                                pres(k), rho\_here, tv%eqn\_of\_state)
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)))) * &
955                 ( 0.5*(dsv\_dt(k-1) + dsv\_dt(k)) * (tf(k-1) - tf(k)) + &
956                   0.5*(dsv\_ds(k-1) + dsv\_ds(k)) * (sf(k-1) - sf(k)) )
957 \textcolor{keywordflow}{      enddo}
958       \textcolor{keyword}{call }post\_data(cs%id\_N2\_f, n2, cs%diag)
959 \textcolor{keywordflow}{    endif}
960 \textcolor{keywordflow}{  endif}
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}}
\subsubsection{\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}
1352   \textcolor{keywordtype}{type}(diapyc\_energy\_req\_CS), \textcolor{keywordtype}{pointer} :: CS\textcolor{comment}{ !< Diapycnal energy requirement control structure that}
1353 \textcolor{comment}{                                            !! will be deallocated in this subroutine.}
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}}
\subsubsection{\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{\tt in}  & {\em time} & model time\\
\hline
\mbox{\tt in}  & {\em g} & model grid structure\\
\hline
\mbox{\tt in}  & {\em gv} & ocean vertical grid structure\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em param\+\_\+file} & file to parse for parameter values\\
\hline
\mbox{\tt 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}
1270   \textcolor{keywordtype}{type}(time\_type),            \textcolor{keywordtype}{intent(in)}    :: Time\textcolor{comment}{        !< model time}
1271   \textcolor{keywordtype}{type}(ocean\_grid\_type),      \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{           !< model grid structure}
1272   \textcolor{keywordtype}{type}(verticalGrid\_type),    \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{          !< ocean vertical grid structure}
1273   \textcolor{keywordtype}{type}(unit\_scale\_type),      \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{          !< A dimensional unit scaling type}
1274   \textcolor{keywordtype}{type}(param\_file\_type),      \textcolor{keywordtype}{intent(in)}    :: param\_file\textcolor{comment}{  !< file to parse for parameter values}
1275   \textcolor{keywordtype}{type}(diag\_ctrl),    \textcolor{keywordtype}{target}, \textcolor{keywordtype}{intent(inout)} :: diag\textcolor{comment}{        !< structure to regulate diagnostic output}
1276   \textcolor{keywordtype}{type}(diapyc\_energy\_req\_CS), \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{          !< module control structure}
1277 
1278   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{save} :: init\_calls = 0
1279 \textcolor{comment}{! This include declares and sets the variable "version".}
1280 \textcolor{preprocessor}{#include "version\_variable.h"}
1281 \textcolor{preprocessor}{}  \textcolor{keywordtype}{character(len=40)}  :: mdl = \textcolor{stringliteral}{"MOM\_diapyc\_energy\_req"} \textcolor{comment}{! This module's name.}
1282   \textcolor{keywordtype}{character(len=256)} :: mesg    \textcolor{comment}{! Message for error messages.}
1283 
1284   \textcolor{keywordflow}{if} (.not.\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{then} ; \textcolor{keyword}{allocate}(cs)
1285   \textcolor{keywordflow}{else} ; \textcolor{keywordflow}{return} ;\textcolor{keywordflow}{ endif}
1286 
1287   cs%initialized = .true.
1288   cs%diag => diag
1289 
1290   \textcolor{comment}{! Read all relevant parameters and write them to the model log.}
1291   \textcolor{keyword}{call }log\_version(param\_file, mdl, version, \textcolor{stringliteral}{""})
1292   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ENERGY\_REQ\_KH\_SCALING"}, cs%test\_Kh\_scaling, &
1293                  \textcolor{stringliteral}{"A scaling factor for the diapycnal diffusivity used in "}//&
1294                  \textcolor{stringliteral}{"testing the energy requirements."}, default=1.0, units=\textcolor{stringliteral}{"nondim"})
1295   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ENERGY\_REQ\_COL\_HT\_SCALING"}, cs%ColHt\_scaling, &
1296                  \textcolor{stringliteral}{"A scaling factor for the column height change correction "}//&
1297                  \textcolor{stringliteral}{"used in testing the energy requirements."}, default=1.0, units=\textcolor{stringliteral}{"nondim"})
1298   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ENERGY\_REQ\_USE\_TEST\_PROFILE"}, &
1299                  cs%use\_test\_Kh\_profile, &
1300                  \textcolor{stringliteral}{"If true, use the internal test diffusivity profile in "}//&
1301                  \textcolor{stringliteral}{"place of any that might be passed in as an argument."}, default=.false.)
1302 
1303   cs%id\_ERt = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_ERt'}, diag%axesZi, time, &
1304                  \textcolor{stringliteral}{"Diffusivity Energy Requirements, top-down"}, &
1305                  \textcolor{stringliteral}{"J m-2"}, conversion=us%RZ\_to\_kg\_m2*us%L\_T\_to\_m\_s**2)
1306   cs%id\_ERb = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_ERb'}, diag%axesZi, time, &
1307                  \textcolor{stringliteral}{"Diffusivity Energy Requirements, bottom-up"}, &
1308                  \textcolor{stringliteral}{"J m-2"}, conversion=us%RZ\_to\_kg\_m2*us%L\_T\_to\_m\_s**2)
1309   cs%id\_ERc = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_ERc'}, diag%axesZi, time, &
1310                  \textcolor{stringliteral}{"Diffusivity Energy Requirements, center-last"}, &
1311                  \textcolor{stringliteral}{"J m-2"}, conversion=us%RZ\_to\_kg\_m2*us%L\_T\_to\_m\_s**2)
1312   cs%id\_ERh = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_ERh'}, diag%axesZi, time, &
1313                  \textcolor{stringliteral}{"Diffusivity Energy Requirements, halves"}, &
1314                  \textcolor{stringliteral}{"J m-2"}, conversion=us%RZ\_to\_kg\_m2*us%L\_T\_to\_m\_s**2)
1315   cs%id\_Kddt = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_Kddt'}, diag%axesZi, time, &
1316                  \textcolor{stringliteral}{"Implicit diffusive coupling coefficient"}, \textcolor{stringliteral}{"m"}, conversion=gv%H\_to\_m)
1317   cs%id\_Kd = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_Kd'}, diag%axesZi, time, &
1318                  \textcolor{stringliteral}{"Diffusivity in test"}, \textcolor{stringliteral}{"m2 s-1"}, conversion=us%Z\_to\_m**2)
1319   cs%id\_h   = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_h\_lay'}, diag%axesZL, time, &
1320                  \textcolor{stringliteral}{"Test column layer thicknesses"}, \textcolor{stringliteral}{"m"}, conversion=gv%H\_to\_m)
1321   cs%id\_zInt = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_z\_int'}, diag%axesZi, time, &
1322                  \textcolor{stringliteral}{"Test column layer interface heights"}, \textcolor{stringliteral}{"m"}, conversion=gv%H\_to\_m)
1323   cs%id\_CHCt = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_CHCt'}, diag%axesZi, time, &
1324                  \textcolor{stringliteral}{"Column Height Correction to Energy Requirements, top-down"}, &
1325                  \textcolor{stringliteral}{"J m-2"}, conversion=us%RZ\_to\_kg\_m2*us%L\_T\_to\_m\_s**2)
1326   cs%id\_CHCb = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_CHCb'}, diag%axesZi, time, &
1327                  \textcolor{stringliteral}{"Column Height Correction to Energy Requirements, bottom-up"}, &
1328                  \textcolor{stringliteral}{"J m-2"}, conversion=us%RZ\_to\_kg\_m2*us%L\_T\_to\_m\_s**2)
1329   cs%id\_CHCc = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_CHCc'}, diag%axesZi, time, &
1330                  \textcolor{stringliteral}{"Column Height Correction to Energy Requirements, center-last"}, &
1331                  \textcolor{stringliteral}{"J m-2"}, conversion=us%RZ\_to\_kg\_m2*us%L\_T\_to\_m\_s**2)
1332   cs%id\_CHCh = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_CHCh'}, diag%axesZi, time, &
1333                  \textcolor{stringliteral}{"Column Height Correction to Energy Requirements, halves"}, &
1334                  \textcolor{stringliteral}{"J m-2"}, conversion=us%RZ\_to\_kg\_m2*us%L\_T\_to\_m\_s**2)
1335   cs%id\_T0 = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_T0'}, diag%axesZL, time, &
1336                  \textcolor{stringliteral}{"Temperature before mixing"}, \textcolor{stringliteral}{"deg C"})
1337   cs%id\_Tf = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_Tf'}, diag%axesZL, time, &
1338                  \textcolor{stringliteral}{"Temperature after mixing"}, \textcolor{stringliteral}{"deg C"})
1339   cs%id\_S0 = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_S0'}, diag%axesZL, time, &
1340                  \textcolor{stringliteral}{"Salinity before mixing"}, \textcolor{stringliteral}{"g kg-1"})
1341   cs%id\_Sf = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_Sf'}, diag%axesZL, time, &
1342                  \textcolor{stringliteral}{"Salinity after mixing"}, \textcolor{stringliteral}{"g kg-1"})
1343   cs%id\_N2\_0 = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_N2\_0'}, diag%axesZi, time, &
1344                  \textcolor{stringliteral}{"Squared buoyancy frequency before mixing"}, \textcolor{stringliteral}{"second-2"}, conversion=us%s\_to\_T**2)
1345   cs%id\_N2\_f = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'EnReqTest\_N2\_f'}, diag%axesZi, time, &
1346                  \textcolor{stringliteral}{"Squared buoyancy frequency after mixing"}, \textcolor{stringliteral}{"second-2"}, conversion=us%s\_to\_T**2)
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}}
\subsubsection{\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{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em h\+\_\+3d} & Layer thickness before entrainment \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em tv} & A structure containing pointers to any available thermodynamic fields. Absent fields have N\+U\+LL ptrs.\\
\hline
\mbox{\tt 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{\tt 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}
50   \textcolor{keywordtype}{type}(ocean\_grid\_type),          \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{    !< The ocean's grid structure.}
51   \textcolor{keywordtype}{type}(verticalGrid\_type),        \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure.}
52   \textcolor{keywordtype}{type}(unit\_scale\_type),          \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{   !< A dimensional unit scaling type}
53   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(G%isd:G%ied,G%jsd:G%jed,GV%ke)}, &
54                                   \textcolor{keywordtype}{intent(in)}    :: h\_3d\textcolor{comment}{ !< Layer thickness before entrainment [H ~> m or kg
       m-2].}
55   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),          \textcolor{keywordtype}{intent(inout)} :: tv\textcolor{comment}{   !< A structure containing pointers to any}
56 \textcolor{comment}{                                                        !! available thermodynamic fields.}
57 \textcolor{comment}{                                                        !! Absent fields have NULL ptrs.}
58   \textcolor{keywordtype}{real},                           \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{   !< The amount of time covered by this call [T ~>
       s].}
59   \textcolor{keywordtype}{type}(diapyc\_energy\_req\_CS),     \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{   !< This module's control structure.}
60   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(G%isd:G%ied,G%jsd:G%jed,GV%ke+1)}, &
61                         \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: Kd\_int\textcolor{comment}{ !< Interface diffusivities [Z2 T-1 ~> m2 s-1].}
62 
63   \textcolor{comment}{! Local variables}
64   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(GV%ke)} :: &
65     T0, S0, &   \textcolor{comment}{! T0 & S0 are columns of initial temperatures and salinities [degC] and g/kg.}
66     h\_col       \textcolor{comment}{! h\_col is a column of thicknesses h at tracer points [H ~> m or kg m-2].}
67   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(GV%ke+1)} :: &
68     Kd, &       \textcolor{comment}{! A column of diapycnal diffusivities at interfaces [Z2 T-1 ~> m2 s-1].}
69     h\_top, h\_bot \textcolor{comment}{! Distances from the top or bottom [H ~> m or kg m-2].}
70   \textcolor{keywordtype}{real} :: ustar, absf, htot
71   \textcolor{keywordtype}{real} :: energy\_Kd \textcolor{comment}{! The energy used by diapycnal mixing [W m-2].}
72   \textcolor{keywordtype}{real} :: tmp1  \textcolor{comment}{! A temporary array.}
73   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, nz, itt
74   \textcolor{keywordtype}{logical} :: may\_print
75   is = g%isc ; ie = g%iec ; js = g%jsc ; je = g%jec ; nz = g%ke
76 
77   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(cs)) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"diapyc\_energy\_req\_test: "}// &
78          \textcolor{stringliteral}{"Module must be initialized before it is used."})
79 
80 \textcolor{comment}{!$OMP do}
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}
82     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_int) .and. .not.cs%use\_test\_Kh\_profile) \textcolor{keywordflow}{then}
83       \textcolor{keywordflow}{do} k=1,nz+1 ; kd(k) = cs%test\_Kh\_scaling*kd\_int(i,j,k) ;\textcolor{keywordflow}{ enddo}
84     \textcolor{keywordflow}{else}
85       htot = 0.0 ; h\_top(1) = 0.0
86       \textcolor{keywordflow}{do} k=1,nz
87         t0(k) = tv%T(i,j,k) ; s0(k) = tv%S(i,j,k)
88         h\_col(k) = h\_3d(i,j,k)
89         h\_top(k+1) = h\_top(k) + h\_col(k)
90 \textcolor{keywordflow}{      enddo}
91       htot = h\_top(nz+1)
92       h\_bot(nz+1) = 0.0
93       \textcolor{keywordflow}{do} k=nz,1,-1
94         h\_bot(k) = h\_bot(k+1) + h\_col(k)
95 \textcolor{keywordflow}{      enddo}
96 
97       ustar = 0.01*us%m\_to\_Z*us%T\_to\_s \textcolor{comment}{! Change this to being an input parameter?}
98       absf = 0.25*((abs(g%CoriolisBu(i-1,j-1)) + abs(g%CoriolisBu(i,j))) + &
99                              (abs(g%CoriolisBu(i-1,j-1)) + abs(g%CoriolisBu(i,j))))
100       kd(1) = 0.0 ; kd(nz+1) = 0.0
101       \textcolor{keywordflow}{do} k=2,nz
102         tmp1 = h\_top(k) * h\_bot(k) * gv%H\_to\_Z
103         kd(k) = cs%test\_Kh\_scaling *  &
104                 ustar * 0.41 * (tmp1*ustar) / (absf*tmp1 + htot*ustar)
105 \textcolor{keywordflow}{      enddo}
106 \textcolor{keywordflow}{    endif}
107     may\_print = is\_root\_pe() .and. (i==ie) .and. (j==je)
108     \textcolor{keyword}{call }diapyc\_energy\_req\_calc(h\_col, t0, s0, kd, energy\_kd, dt, tv, g, gv, us, &
109                                 may\_print=may\_print, cs=cs)
110 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
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}}
\subsubsection{\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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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}
970   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: Kddt\_h0\textcolor{comment}{  !< The previously used diffusivity at an interface times}
971 \textcolor{comment}{                                !! the time step and  divided by the average of the}
972 \textcolor{comment}{                                !! thicknesses around the interface [H ~> m or kg m-2].}
973   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dKddt\_h\textcolor{comment}{  !< The trial change in the diffusivity at an interface times}
974 \textcolor{comment}{                                !! the time step and  divided by the average of the}
975 \textcolor{comment}{                                !! thicknesses around the interface [H ~> m or kg m-2].}
976   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: hp\_a\textcolor{comment}{     !< The effective pivot thickness of the layer above the}
977 \textcolor{comment}{                                !! interface, given by h\_k plus a term that}
978 \textcolor{comment}{                                !! is a fraction (determined from the tridiagonal solver) of}
979 \textcolor{comment}{                                !! Kddt\_h for the interface above [H ~> m or kg m-2].}
980   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: hp\_b\textcolor{comment}{     !< The effective pivot thickness of the layer below the}
981 \textcolor{comment}{                                !! interface, given by h\_k plus a term that}
982 \textcolor{comment}{                                !! is a fraction (determined from the tridiagonal solver) of}
983 \textcolor{comment}{                                !! Kddt\_h for the interface above [H ~> m or kg m-2].}
984   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: Th\_a\textcolor{comment}{     !< An effective temperature times a thickness in the layer}
985 \textcolor{comment}{                                !! above, including implicit mixing effects with other}
986 \textcolor{comment}{                                !! yet higher layers [degC H ~> degC m or degC kg m-2].}
987   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: Sh\_a\textcolor{comment}{     !< An effective salinity times a thickness in the layer}
988 \textcolor{comment}{                                !! above, including implicit mixing effects with other}
989 \textcolor{comment}{                                !! yet higher layers [ppt H ~> ppt m or ppt kg m-2].}
990   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: Th\_b\textcolor{comment}{     !< An effective temperature times a thickness in the layer}
991 \textcolor{comment}{                                !! below, including implicit mixing effects with other}
992 \textcolor{comment}{                                !! yet lower layers [degC H ~> degC m or degC kg m-2].}
993   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: Sh\_b\textcolor{comment}{     !< An effective salinity times a thickness in the layer}
994 \textcolor{comment}{                                !! below, including implicit mixing effects with other}
995 \textcolor{comment}{                                !! yet lower layers [ppt H ~> ppt m or ppt kg m-2].}
996   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dT\_to\_dPE\_a\textcolor{comment}{ !< A factor (pres\_lay*mass\_lay*dSpec\_vol/dT) relating}
997 \textcolor{comment}{                                !! a layer's temperature change to the change in column}
998 \textcolor{comment}{                                !! potential energy, including all implicit diffusive changes}
999 \textcolor{comment}{                                !! in the temperatures of all the layers above [R Z L2 T-2 degC-1 ~> J m-2
       degC-1].}
1000   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dS\_to\_dPE\_a\textcolor{comment}{ !< A factor (pres\_lay*mass\_lay*dSpec\_vol/dS) relating}
1001 \textcolor{comment}{                                !! a layer's salinity change to the change in column}
1002 \textcolor{comment}{                                !! potential energy, including all implicit diffusive changes}
1003 \textcolor{comment}{                                !! in the salinities of all the layers above [R Z L2 T-2 ppt-1 ~> J m-2
       ppt-1].}
1004   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dT\_to\_dPE\_b\textcolor{comment}{ !< A factor (pres\_lay*mass\_lay*dSpec\_vol/dT) relating}
1005 \textcolor{comment}{                                !! a layer's temperature change to the change in column}
1006 \textcolor{comment}{                                !! potential energy, including all implicit diffusive changes}
1007 \textcolor{comment}{                                !! in the temperatures of all the layers below [R Z L2 T-2 degC-1 ~> J m-2
       degC-1].}
1008   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dS\_to\_dPE\_b\textcolor{comment}{ !< A factor (pres\_lay*mass\_lay*dSpec\_vol/dS) relating}
1009 \textcolor{comment}{                                !! a layer's salinity change to the change in column}
1010 \textcolor{comment}{                                !! potential energy, including all implicit diffusive changes}
1011 \textcolor{comment}{                                !! in the salinities of all the layers below [R Z L2 T-2 ppt-1 ~> J m-2
       ppt-1].}
1012   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: pres\_Z\textcolor{comment}{   !< The hydrostatic interface pressure, which is used to relate}
1013 \textcolor{comment}{                                !! the changes in column thickness to the energy that is radiated}
1014 \textcolor{comment}{                                !! as gravity waves and unavailable to drive mixing [R L2 T-2 m Z-1 ~> J
       m-3].}
1015   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dT\_to\_dColHt\_a\textcolor{comment}{ !< A factor (mass\_lay*dSColHtc\_vol/dT) relating}
1016 \textcolor{comment}{                                !! a layer's temperature change to the change in column}
1017 \textcolor{comment}{                                !! height, including all implicit diffusive changes}
1018 \textcolor{comment}{                                !! in the temperatures of all the layers above [Z degC-1 ~> m degC-1].}
1019   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dS\_to\_dColHt\_a\textcolor{comment}{ !< A factor (mass\_lay*dSColHtc\_vol/dS) relating}
1020 \textcolor{comment}{                                !! a layer's salinity change to the change in column}
1021 \textcolor{comment}{                                !! height, including all implicit diffusive changes}
1022 \textcolor{comment}{                                !! in the salinities of all the layers above [Z ppt-1 ~> m ppt-1].}
1023   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dT\_to\_dColHt\_b\textcolor{comment}{ !< A factor (mass\_lay*dSColHtc\_vol/dT) relating}
1024 \textcolor{comment}{                                !! a layer's temperature change to the change in column}
1025 \textcolor{comment}{                                !! height, including all implicit diffusive changes}
1026 \textcolor{comment}{                                !! in the temperatures of all the layers below [Z degC-1 ~> m degC-1].}
1027   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dS\_to\_dColHt\_b\textcolor{comment}{ !< A factor (mass\_lay*dSColHtc\_vol/dS) relating}
1028 \textcolor{comment}{                                !! a layer's salinity change to the change in column}
1029 \textcolor{comment}{                                !! height, including all implicit diffusive changes}
1030 \textcolor{comment}{                                !! in the salinities of all the layers below [Z ppt-1 ~> m ppt-1].}
1031 
1032   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: PE\_chg\textcolor{comment}{   !< The change in column potential energy from applying}
1033 \textcolor{comment}{                                          !! Kddt\_h at the present interface [R Z L2 T-2 ~> J m-2].}
1034   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: dPEc\_dKd\textcolor{comment}{ !< The partial derivative of PE\_chg with Kddt\_h,}
1035 \textcolor{comment}{                                          !! [R Z L2 T-2 H-1 ~> J m-3 or J kg-1].}
1036   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: dPE\_max\textcolor{comment}{  !< The maximum change in column potential energy that could}
1037 \textcolor{comment}{                                          !! be realizedd by applying a huge value of Kddt\_h at the}
1038 \textcolor{comment}{                                          !! present interface [R Z L2 T-2 ~> J m-2].}
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}
1040 \textcolor{comment}{                                            !! limit where Kddt\_h = 0 [R Z L2 T-2 H-1 ~> J m-3 or J kg-1].}
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}
1042 \textcolor{comment}{                                            !! change in the column height [R Z L2 T-2 ~> J m-2].}
1043 
1044   \textcolor{keywordtype}{real} :: hps  \textcolor{comment}{! The sum of the two effective pivot thicknesses [H ~> m or kg m-2].}
1045   \textcolor{keywordtype}{real} :: bdt1 \textcolor{comment}{! A product of the two pivot thicknesses plus a diffusive term [H2 ~> m2 or kg2 m-4].}
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].}
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].}
1048   \textcolor{keywordtype}{real} :: PEc\_core \textcolor{comment}{! The diffusivity-independent core term in the expressions}
1049                    \textcolor{comment}{! for the potential energy changes [R L2 T-2 ~> J m-3].}
1050   \textcolor{keywordtype}{real} :: ColHt\_core \textcolor{comment}{! The diffusivity-independent core term in the expressions}
1051                      \textcolor{comment}{! for the column height changes [R L2 T-2 ~> J m-3].}
1052   \textcolor{keywordtype}{real} :: ColHt\_chg  \textcolor{comment}{! The change in the column height [Z ~> m].}
1053   \textcolor{keywordtype}{real} :: y1   \textcolor{comment}{! A local temporary term, in [H-3] or [H-4] in various contexts.}
1054 
1055   \textcolor{comment}{!   The expression for the change in potential energy used here is derived}
1056   \textcolor{comment}{! from the expression for the final estimates of the changes in temperature}
1057   \textcolor{comment}{! and salinities, and then extensively manipulated to get it into its most}
1058   \textcolor{comment}{! succint form. The derivation is not necessarily obvious, but it demonstrably}
1059   \textcolor{comment}{! works by comparison with separate calculations of the energy changes after}
1060   \textcolor{comment}{! the tridiagonal solver for the final changes in temperature and salinity are}
1061   \textcolor{comment}{! applied.}
1062 
1063   hps = hp\_a + hp\_b
1064   bdt1 = hp\_a * hp\_b + kddt\_h0 * hps
1065   dt\_c = hp\_a * th\_b - hp\_b * th\_a
1066   ds\_c = hp\_a * sh\_b - hp\_b * sh\_a
1067   pec\_core = hp\_b * (dt\_to\_dpe\_a * dt\_c + ds\_to\_dpe\_a * ds\_c) - &
1068              hp\_a * (dt\_to\_dpe\_b * dt\_c + ds\_to\_dpe\_b * ds\_c)
1069   colht\_core = hp\_b * (dt\_to\_dcolht\_a * dt\_c + ds\_to\_dcolht\_a * ds\_c) - &
1070                hp\_a * (dt\_to\_dcolht\_b * dt\_c + ds\_to\_dcolht\_b * ds\_c)
1071 
1072   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(pe\_chg)) \textcolor{keywordflow}{then}
1073     \textcolor{comment}{! Find the change in column potential energy due to the change in the}
1074     \textcolor{comment}{! diffusivity at this interface by dKddt\_h.}
1075     y1 = dkddt\_h / (bdt1 * (bdt1 + dkddt\_h * hps))
1076     pe\_chg = pec\_core * y1
1077     colht\_chg = colht\_core * y1
1078     \textcolor{keywordflow}{if} (colht\_chg < 0.0) pe\_chg = pe\_chg - pres\_z * colht\_chg
1079     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(colht\_cor)) colht\_cor = -pres\_z * min(colht\_chg, 0.0)
1080   \textcolor{keywordflow}{elseif} (\textcolor{keyword}{present}(colht\_cor)) \textcolor{keywordflow}{then}
1081     y1 = dkddt\_h / (bdt1 * (bdt1 + dkddt\_h * hps))
1082     colht\_cor = -pres\_z * min(colht\_core * y1, 0.0)
1083 \textcolor{keywordflow}{  endif}
1084 
1085   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dpec\_dkd)) \textcolor{keywordflow}{then}
1086     \textcolor{comment}{! Find the derivative of the potential energy change with dKddt\_h.}
1087     y1 = 1.0 / (bdt1 + dkddt\_h * hps)**2
1088     dpec\_dkd = pec\_core * y1
1089     colht\_chg = colht\_core * y1
1090     \textcolor{keywordflow}{if} (colht\_chg < 0.0) dpec\_dkd = dpec\_dkd - pres\_z * colht\_chg
1091 \textcolor{keywordflow}{  endif}
1092 
1093   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dpe\_max)) \textcolor{keywordflow}{then}
1094     \textcolor{comment}{! This expression is the limit of PE\_chg for infinite dKddt\_h.}
1095     y1 = 1.0 / (bdt1 * hps)
1096     dpe\_max = pec\_core * y1
1097     colht\_chg = colht\_core * y1
1098     \textcolor{keywordflow}{if} (colht\_chg < 0.0) dpe\_max = dpe\_max - pres\_z * colht\_chg
1099 \textcolor{keywordflow}{  endif}
1100 
1101   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dpec\_dkd\_0)) \textcolor{keywordflow}{then}
1102     \textcolor{comment}{! This expression is the limit of dPEc\_dKd for dKddt\_h = 0.}
1103     y1 = 1.0 / bdt1**2
1104     dpec\_dkd\_0 = pec\_core * y1
1105     colht\_chg = colht\_core * y1
1106     \textcolor{keywordflow}{if} (colht\_chg < 0.0) dpec\_dkd\_0 = dpec\_dkd\_0 - pres\_z * colht\_chg
1107 \textcolor{keywordflow}{  endif}
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}}
\subsubsection{\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{\tt 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{\tt in}  & {\em h\+\_\+k} & The thickness of the layer below the interface \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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}
1120   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: Kddt\_h\textcolor{comment}{   !< The diffusivity at an interface times the time step and}
1121 \textcolor{comment}{                                !! divided by the average of the thicknesses around the}
1122 \textcolor{comment}{                                !! interface [H ~> m or kg m-2].}
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].}
1124   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: b\_den\_1\textcolor{comment}{  !< The first term in the denominator of the pivot}
1125 \textcolor{comment}{                                !! for the tridiagonal solver, given by h\_k plus a term that}
1126 \textcolor{comment}{                                !! is a fraction (determined from the tridiagonal solver) of}
1127 \textcolor{comment}{                                !! Kddt\_h for the interface above [H ~> m or kg m-2].}
1128   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dTe\_term\textcolor{comment}{ !< A diffusivity-independent term related to the temperature change}
1129 \textcolor{comment}{                                !! in the layer below the interface [degC H ~> degC m or degC kg m-2].}
1130   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dSe\_term\textcolor{comment}{ !< A diffusivity-independent term related to the salinity change}
1131 \textcolor{comment}{                                !! in the layer below the interface [ppt H ~> ppt m or ppt kg m-2].}
1132   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dT\_km1\_t2\textcolor{comment}{ !< A diffusivity-independent term related to the}
1133 \textcolor{comment}{                                 !! temperature change in the layer above the interface [degC].}
1134   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dS\_km1\_t2\textcolor{comment}{ !< A diffusivity-independent term related to the}
1135 \textcolor{comment}{                                 !! salinity change in the layer above the interface [ppt].}
1136   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: pres\_Z\textcolor{comment}{    !< The hydrostatic interface pressure, which is used to relate}
1137 \textcolor{comment}{                                 !! the changes in column thickness to the energy that is radiated}
1138 \textcolor{comment}{                                 !! as gravity waves and unavailable to drive mixing [R L2 T-2 ~> J m-3].}
1139   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dT\_to\_dPE\_k\textcolor{comment}{ !< A factor (pres\_lay*mass\_lay*dSpec\_vol/dT) relating}
1140 \textcolor{comment}{                                 !! a layer's temperature change to the change in column}
1141 \textcolor{comment}{                                 !! potential energy, including all implicit diffusive changes}
1142 \textcolor{comment}{                                 !! in the temperatures of all the layers below [R Z L2 T-2 degC-1 ~> J m-2
       degC-1].}
1143   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dS\_to\_dPE\_k\textcolor{comment}{ !< A factor (pres\_lay*mass\_lay*dSpec\_vol/dS) relating}
1144 \textcolor{comment}{                                 !! a layer's salinity change to the change in column}
1145 \textcolor{comment}{                                 !! potential energy, including all implicit diffusive changes}
1146 \textcolor{comment}{                                 !! in the salinities of all the layers below [R Z L2 T-2 ppt-1 ~> J m-2
       ppt-1].}
1147   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dT\_to\_dPEa\textcolor{comment}{ !< A factor (pres\_lay*mass\_lay*dSpec\_vol/dT) relating}
1148 \textcolor{comment}{                                 !! a layer's temperature change to the change in column}
1149 \textcolor{comment}{                                 !! potential energy, including all implicit diffusive changes}
1150 \textcolor{comment}{                                 !! in the temperatures of all the layers above [R Z L2 T-2 degC-1 ~> J m-2
       degC-1].}
1151   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dS\_to\_dPEa\textcolor{comment}{ !< A factor (pres\_lay*mass\_lay*dSpec\_vol/dS) relating}
1152 \textcolor{comment}{                                 !! a layer's salinity change to the change in column}
1153 \textcolor{comment}{                                 !! potential energy, including all implicit diffusive changes}
1154 \textcolor{comment}{                                 !! in the salinities of all the layers above [R Z L2 T-2 ppt-1 ~> J m-2
       ppt-1].}
1155   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dT\_to\_dColHt\_k\textcolor{comment}{ !< A factor (mass\_lay*dSColHtc\_vol/dT) relating}
1156 \textcolor{comment}{                                 !! a layer's temperature change to the change in column}
1157 \textcolor{comment}{                                 !! height, including all implicit diffusive changes}
1158 \textcolor{comment}{                                 !! in the temperatures of all the layers below [Z degC-1 ~> m degC-1].}
1159   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dS\_to\_dColHt\_k\textcolor{comment}{ !< A factor (mass\_lay*dSColHtc\_vol/dS) relating}
1160 \textcolor{comment}{                                 !! a layer's salinity change to the change in column}
1161 \textcolor{comment}{                                 !! height, including all implicit diffusive changes}
1162 \textcolor{comment}{                                 !! in the salinities of all the layers below [Z ppt-1 ~> m ppt-1].}
1163   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dT\_to\_dColHta\textcolor{comment}{ !< A factor (mass\_lay*dSColHtc\_vol/dT) relating}
1164 \textcolor{comment}{                                 !! a layer's temperature change to the change in column}
1165 \textcolor{comment}{                                 !! height, including all implicit diffusive changes}
1166 \textcolor{comment}{                                 !! in the temperatures of all the layers above [Z degC-1 ~> m degC-1].}
1167   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)}  :: dS\_to\_dColHta\textcolor{comment}{ !< A factor (mass\_lay*dSColHtc\_vol/dS) relating}
1168 \textcolor{comment}{                                 !! a layer's salinity change to the change in column}
1169 \textcolor{comment}{                                 !! height, including all implicit diffusive changes}
1170 \textcolor{comment}{                                 !! in the salinities of all the layers above [Z ppt-1 ~> m ppt-1].}
1171 
1172   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: PE\_chg\textcolor{comment}{   !< The change in column potential energy from applying}
1173 \textcolor{comment}{                                          !! Kddt\_h at the present interface [R Z L2 T-2 ~> J m-2].}
1174   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: dPEc\_dKd\textcolor{comment}{ !< The partial derivative of PE\_chg with Kddt\_h,}
1175 \textcolor{comment}{                                          !! [R Z L2 T-2 H-1 ~> J m-3 or J kg-1].}
1176   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: dPE\_max\textcolor{comment}{  !< The maximum change in column potential energy that could}
1177 \textcolor{comment}{                                          !! be realized by applying a huge value of Kddt\_h at the}
1178 \textcolor{comment}{                                          !! present interface [R Z L2 T-2 ~> J m-2].}
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}
1180 \textcolor{comment}{                                            !! limit where Kddt\_h = 0 [R Z L2 T-2 H-1 ~> J m-3 or J kg-1].}
1181 
1182 \textcolor{comment}{!   This subroutine determines the total potential energy change due to mixing}
1183 \textcolor{comment}{! at an interface, including all of the implicit effects of the prescribed}
1184 \textcolor{comment}{! mixing at interfaces above.  Everything here is derived by careful manipulation}
1185 \textcolor{comment}{! of the robust tridiagonal solvers used for tracers by MOM6.  The results are}
1186 \textcolor{comment}{! positive for mixing in a stably stratified environment.}
1187 \textcolor{comment}{!   The comments describing these arguments are for a downward mixing pass, but}
1188 \textcolor{comment}{! this routine can also be used for an upward pass with the sense of direction}
1189 \textcolor{comment}{! reversed.}
1190 
1191   \textcolor{keywordtype}{real} :: b1            \textcolor{comment}{! b1 is used by the tridiagonal solver [H-1 ~> m-1 or m2 kg-1].}
1192   \textcolor{keywordtype}{real} :: b1Kd          \textcolor{comment}{! Temporary array [nondim]}
1193   \textcolor{keywordtype}{real} :: ColHt\_chg     \textcolor{comment}{! The change in column thickness [Z ~> m].}
1194   \textcolor{keywordtype}{real} :: dColHt\_max    \textcolor{comment}{! The change in column thickness for infinite diffusivity [Z ~> m].}
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].}
1196   \textcolor{keywordtype}{real} :: dT\_k, dT\_km1  \textcolor{comment}{! Temporary arrays [degC].}
1197   \textcolor{keywordtype}{real} :: dS\_k, dS\_km1  \textcolor{comment}{! Temporary arrays [ppt].}
1198   \textcolor{keywordtype}{real} :: I\_Kr\_denom    \textcolor{comment}{! Temporary arrays [H-2 ~> m-2 or m4 kg-2].}
1199   \textcolor{keywordtype}{real} :: dKr\_dKd       \textcolor{comment}{! Nondimensional temporary array [nondim].}
1200   \textcolor{keywordtype}{real} :: ddT\_k\_dKd, ddT\_km1\_dKd \textcolor{comment}{! Temporary arrays [degC H-1 ~> m-1 or m2 kg-1].}
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].}
1202 
1203   b1 = 1.0 / (b\_den\_1 + kddt\_h)
1204   b1kd = kddt\_h*b1
1205 
1206   \textcolor{comment}{! Start with the temperature change in layer k-1 due to the diffusivity at}
1207   \textcolor{comment}{! interface K without considering the effects of changes in layer k.}
1208 
1209   \textcolor{comment}{! Calculate the change in PE due to the diffusion at interface K}
1210   \textcolor{comment}{! if Kddt\_h(K+1) = 0.}
1211   i\_kr\_denom = 1.0 / (h\_k*b\_den\_1 + (b\_den\_1 + h\_k)*kddt\_h)
1212 
1213   dt\_k = (kddt\_h*i\_kr\_denom) * dte\_term
1214   ds\_k = (kddt\_h*i\_kr\_denom) * dse\_term
1215 
1216   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(pe\_chg)) \textcolor{keywordflow}{then}
1217     \textcolor{comment}{! Find the change in energy due to diffusion with strength Kddt\_h at this interface.}
1218     \textcolor{comment}{! Increment the temperature changes in layer k-1 due the changes in layer k.}
1219     dt\_km1 = b1kd * ( dt\_k + dt\_km1\_t2 )
1220     ds\_km1 = b1kd * ( ds\_k + ds\_km1\_t2 )
1221 
1222     pe\_chg = (dt\_to\_dpe\_k * dt\_k + dt\_to\_dpea * dt\_km1) + &
1223              (ds\_to\_dpe\_k * ds\_k + ds\_to\_dpea * ds\_km1)
1224     colht\_chg = (dt\_to\_dcolht\_k * dt\_k + dt\_to\_dcolhta * dt\_km1) + &
1225                 (ds\_to\_dcolht\_k * ds\_k + ds\_to\_dcolhta * ds\_km1)
1226     \textcolor{keywordflow}{if} (colht\_chg < 0.0) pe\_chg = pe\_chg - pres\_z * colht\_chg
1227 \textcolor{keywordflow}{  endif}
1228 
1229   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dpec\_dkd)) \textcolor{keywordflow}{then}
1230     \textcolor{comment}{! Find the derivatives of the temperature and salinity changes with Kddt\_h.}
1231     dkr\_dkd = (h\_k*b\_den\_1) * i\_kr\_denom**2
1232 
1233     ddt\_k\_dkd = dkr\_dkd * dte\_term
1234     dds\_k\_dkd = dkr\_dkd * dse\_term
1235     ddt\_km1\_dkd = (b1**2 * b\_den\_1) * ( dt\_k + dt\_km1\_t2 ) + b1kd * ddt\_k\_dkd
1236     dds\_km1\_dkd = (b1**2 * b\_den\_1) * ( ds\_k + ds\_km1\_t2 ) + b1kd * dds\_k\_dkd
1237 
1238     \textcolor{comment}{! Calculate the partial derivative of Pe\_chg with Kddt\_h.}
1239     dpec\_dkd = (dt\_to\_dpe\_k * ddt\_k\_dkd + dt\_to\_dpea * ddt\_km1\_dkd) + &
1240                (ds\_to\_dpe\_k * dds\_k\_dkd + ds\_to\_dpea * dds\_km1\_dkd)
1241     dcolht\_dkd = (dt\_to\_dcolht\_k * ddt\_k\_dkd + dt\_to\_dcolhta * ddt\_km1\_dkd) + &
1242                  (ds\_to\_dcolht\_k * dds\_k\_dkd + ds\_to\_dcolhta * dds\_km1\_dkd)
1243     \textcolor{keywordflow}{if} (dcolht\_dkd < 0.0) dpec\_dkd = dpec\_dkd - pres\_z * dcolht\_dkd
1244 \textcolor{keywordflow}{  endif}
1245 
1246   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dpe\_max)) \textcolor{keywordflow}{then}
1247     \textcolor{comment}{! This expression is the limit of PE\_chg for infinite Kddt\_h.}
1248     dpe\_max = (dt\_to\_dpea * dt\_km1\_t2 + ds\_to\_dpea * ds\_km1\_t2) + &
1249               ((dt\_to\_dpe\_k + dt\_to\_dpea) * dte\_term + &
1250                (ds\_to\_dpe\_k + ds\_to\_dpea) * dse\_term) / (b\_den\_1 + h\_k)
1251     dcolht\_max = (dt\_to\_dcolhta * dt\_km1\_t2 + ds\_to\_dcolhta * ds\_km1\_t2) + &
1252               ((dt\_to\_dcolht\_k + dt\_to\_dcolhta) * dte\_term + &
1253                (ds\_to\_dcolht\_k + ds\_to\_dcolhta) * dse\_term) / (b\_den\_1 + h\_k)
1254     \textcolor{keywordflow}{if} (dcolht\_max < 0.0) dpe\_max = dpe\_max - pres\_z*dcolht\_max
1255 \textcolor{keywordflow}{  endif}
1256 
1257   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dpec\_dkd\_0)) \textcolor{keywordflow}{then}
1258     \textcolor{comment}{! This expression is the limit of dPEc\_dKd for Kddt\_h = 0.}
1259     dpec\_dkd\_0 = (dt\_to\_dpea * dt\_km1\_t2 + ds\_to\_dpea * ds\_km1\_t2) / (b\_den\_1) + &
1260                  (dt\_to\_dpe\_k * dte\_term + ds\_to\_dpe\_k * dse\_term) / (h\_k*b\_den\_1)
1261     dcolht\_dkd = (dt\_to\_dcolhta * dt\_km1\_t2 + ds\_to\_dcolhta * ds\_km1\_t2) / (b\_den\_1) + &
1262                  (dt\_to\_dcolht\_k * dte\_term + ds\_to\_dcolht\_k * dse\_term) / (h\_k*b\_den\_1)
1263     \textcolor{keywordflow}{if} (dcolht\_dkd < 0.0) dpec\_dkd\_0 = dpec\_dkd\_0 - pres\_z*dcolht\_dkd
1264 \textcolor{keywordflow}{  endif}
1265 
\end{DoxyCode}
