\hypertarget{namespacecoord__rho}{}\section{coord\+\_\+rho Module Reference}
\label{namespacecoord__rho}\index{coord\+\_\+rho@{coord\+\_\+rho}}


\subsection{Detailed Description}
Regrid columns for the continuous isopycnal (rho) coordinate. \subsection*{Data Types}
\begin{DoxyCompactItemize}
\item 
type \hyperlink{structcoord__rho_1_1rho__cs}{rho\+\_\+cs}
\begin{DoxyCompactList}\small\item\em Control structure containing required parameters for the rho coordinate. \end{DoxyCompactList}\end{DoxyCompactItemize}
\subsection*{Functions/\+Subroutines}
\begin{DoxyCompactItemize}
\item 
subroutine, public \hyperlink{namespacecoord__rho_a86640b2be846b291c71f1c38b8ff9878}{init\+\_\+coord\+\_\+rho} (CS, nk, ref\+\_\+pressure, target\+\_\+density, interp\+\_\+\+CS)
\begin{DoxyCompactList}\small\item\em Initialise a rho\+\_\+\+CS with pointers to parameters. \end{DoxyCompactList}\item 
subroutine, public \hyperlink{namespacecoord__rho_a8e9c666d47d847ab8b64a22c470931d1}{end\+\_\+coord\+\_\+rho} (CS)
\begin{DoxyCompactList}\small\item\em This subroutine deallocates memory in the control structure for the \hyperlink{namespacecoord__rho}{coord\+\_\+rho} module. \end{DoxyCompactList}\item 
subroutine, public \hyperlink{namespacecoord__rho_a6b2a48523247f59c7b4b5b7941334c25}{set\+\_\+rho\+\_\+params} (CS, min\+\_\+thickness, integrate\+\_\+downward\+\_\+for\+\_\+e, interp\+\_\+\+CS)
\begin{DoxyCompactList}\small\item\em This subroutine can be used to set the parameters for the \hyperlink{namespacecoord__rho}{coord\+\_\+rho} module. \end{DoxyCompactList}\item 
subroutine, public \hyperlink{namespacecoord__rho_ac3f19bfa0e6b622cdf045c348ec4d183}{build\+\_\+rho\+\_\+column} (CS, nz, depth, h, T, S, eqn\+\_\+of\+\_\+state, z\+\_\+interface, h\+\_\+neglect, h\+\_\+neglect\+\_\+edge)
\begin{DoxyCompactList}\small\item\em Build a rho coordinate column. \end{DoxyCompactList}\item 
subroutine \hyperlink{namespacecoord__rho_ad79c2a471c8de0b16f0478813be7c29f}{build\+\_\+rho\+\_\+column\+\_\+iteratively} (CS, remap\+CS, nz, depth, h, T, S, eqn\+\_\+of\+\_\+state, z\+Interface, h\+\_\+neglect, h\+\_\+neglect\+\_\+edge, dev\+\_\+tol)
\begin{DoxyCompactList}\small\item\em Iteratively build a rho coordinate column. \end{DoxyCompactList}\item 
subroutine \hyperlink{namespacecoord__rho_a17064808dccea1f725fe82cd49cd2541}{copy\+\_\+finite\+\_\+thicknesses} (nk, h\+\_\+in, thresh, nout, h\+\_\+out, mapping)
\begin{DoxyCompactList}\small\item\em Copy column thicknesses with vanished layers removed. \end{DoxyCompactList}\item 
subroutine, public \hyperlink{namespacecoord__rho_afbc86f29df8e39eb793d12075d018518}{old\+\_\+inflate\+\_\+layers\+\_\+1d} (min\+\_\+thickness, nk, h)
\begin{DoxyCompactList}\small\item\em Inflate vanished layers to finite (nonzero) width. \end{DoxyCompactList}\end{DoxyCompactItemize}


\subsection{Function/\+Subroutine Documentation}
\mbox{\Hypertarget{namespacecoord__rho_ac3f19bfa0e6b622cdf045c348ec4d183}\label{namespacecoord__rho_ac3f19bfa0e6b622cdf045c348ec4d183}} 
\index{coord\+\_\+rho@{coord\+\_\+rho}!build\+\_\+rho\+\_\+column@{build\+\_\+rho\+\_\+column}}
\index{build\+\_\+rho\+\_\+column@{build\+\_\+rho\+\_\+column}!coord\+\_\+rho@{coord\+\_\+rho}}
\subsubsection{\texorpdfstring{build\+\_\+rho\+\_\+column()}{build\_rho\_column()}}
{\footnotesize\ttfamily subroutine, public coord\+\_\+rho\+::build\+\_\+rho\+\_\+column (\begin{DoxyParamCaption}\item[{type(\hyperlink{structcoord__rho_1_1rho__cs}{rho\+\_\+cs}), intent(in)}]{CS,  }\item[{integer, intent(in)}]{nz,  }\item[{real, intent(in)}]{depth,  }\item[{real, dimension(nz), intent(in)}]{h,  }\item[{real, dimension(nz), intent(in)}]{T,  }\item[{real, dimension(nz), intent(in)}]{S,  }\item[{type(eos\+\_\+type), pointer}]{eqn\+\_\+of\+\_\+state,  }\item[{real, dimension(cs\%nk+1), intent(inout)}]{z\+\_\+interface,  }\item[{real, intent(in), optional}]{h\+\_\+neglect,  }\item[{real, intent(in), optional}]{h\+\_\+neglect\+\_\+edge }\end{DoxyParamCaption})}



Build a rho coordinate column. 


\begin{DoxyEnumerate}
\item Density profiles are calculated on the source grid.
\item Positions of target densities (for interfaces) are found by interpolation.
\end{DoxyEnumerate}


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em cs} & \hyperlink{namespacecoord__rho}{coord\+\_\+rho} control structure\\
\hline
\mbox{\tt in}  & {\em nz} & Number of levels on source grid (i.\+e. length of h, T, S)\\
\hline
\mbox{\tt in}  & {\em depth} & Depth of ocean bottom (positive downward) \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em t} & Temperature for source column \mbox{[}degC\mbox{]}\\
\hline
\mbox{\tt in}  & {\em s} & Salinity for source column \mbox{[}ppt\mbox{]}\\
\hline
 & {\em eqn\+\_\+of\+\_\+state} & Equation of state structure\\
\hline
\mbox{\tt in,out}  & {\em z\+\_\+interface} & Absolute positions of interfaces\\
\hline
\mbox{\tt in}  & {\em h\+\_\+neglect} & A negligibly small width for the purpose of cell reconstructions \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em h\+\_\+neglect\+\_\+edge} & A negligibly small width for the purpose of edge value calculations \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\end{DoxyParams}


Definition at line 92 of file coord\+\_\+rho.\+F90.


\begin{DoxyCode}
92   \textcolor{keywordtype}{type}(rho\_cs),        \textcolor{keywordtype}{intent(in)}    :: cs\textcolor{comment}{ !< coord\_rho control structure}
93   \textcolor{keywordtype}{integer},             \textcolor{keywordtype}{intent(in)}    :: nz\textcolor{comment}{ !< Number of levels on source grid (i.e. length of  h, T, S)}
94   \textcolor{keywordtype}{real},                \textcolor{keywordtype}{intent(in)}    :: depth\textcolor{comment}{ !< Depth of ocean bottom (positive downward) [H ~> m or kg
       m-2]}
95   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nz)}, \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{  !< Layer thicknesses [H ~> m or kg m-2]}
96   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nz)}, \textcolor{keywordtype}{intent(in)}    :: t\textcolor{comment}{  !< Temperature for source column [degC]}
97   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nz)}, \textcolor{keywordtype}{intent(in)}    :: s\textcolor{comment}{  !< Salinity for source column [ppt]}
98   \textcolor{keywordtype}{type}(eos\_type),      \textcolor{keywordtype}{pointer}       :: eqn\_of\_state\textcolor{comment}{ !< Equation of state structure}
99   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(CS%nk+1)}, &
100                        \textcolor{keywordtype}{intent(inout)} :: z\_interface\textcolor{comment}{ !< Absolute positions of interfaces}
101   \textcolor{keywordtype}{real},      \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: h\_neglect\textcolor{comment}{ !< A negligibly small width for the purpose}
102 \textcolor{comment}{                                             !! of cell reconstructions [H ~> m or kg m-2]}
103   \textcolor{keywordtype}{real},      \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: h\_neglect\_edge\textcolor{comment}{ !< A negligibly small width for the purpose}
104 \textcolor{comment}{                                             !! of edge value calculations [H ~> m or kg m-2]}
105 
106   \textcolor{comment}{! Local variables}
107   \textcolor{keywordtype}{integer} :: k, count\_nonzero\_layers
108   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(nz)} :: mapping
109   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nz)} :: pres     \textcolor{comment}{! Pressures used to calculate density [R L2 T-2 ~> Pa]}
110   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nz)} :: h\_nv     \textcolor{comment}{! Thicknesses of non-vanishing layers [H ~> m or kg m-2]}
111   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nz)} :: densities \textcolor{comment}{! Layer density [R ~> kg m-3]}
112   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nz+1)} :: xtmp   \textcolor{comment}{! Temporary positions [H ~> m or kg m-2]}
113   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(CS%nk)} :: h\_new \textcolor{comment}{! New thicknesses [H ~> m or kg m-2]}
114   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(CS%nk+1)} :: x1  \textcolor{comment}{! Interface heights [H ~> m or kg m-2]}
115 
116   \textcolor{comment}{! Construct source column with vanished layers removed (stored in h\_nv)}
117   \textcolor{keyword}{call }copy\_finite\_thicknesses(nz, h, cs%min\_thickness, count\_nonzero\_layers, h\_nv, mapping)
118 
119   \textcolor{keywordflow}{if} (count\_nonzero\_layers > 1) \textcolor{keywordflow}{then}
120     xtmp(1) = 0.0
121     \textcolor{keywordflow}{do} k = 1,count\_nonzero\_layers
122       xtmp(k+1) = xtmp(k) + h\_nv(k)
123 \textcolor{keywordflow}{    enddo}
124 
125     \textcolor{comment}{! Compute densities on source column}
126     pres(:) = cs%ref\_pressure
127     \textcolor{keyword}{call }calculate\_density(t, s, pres, densities, eqn\_of\_state)
128     \textcolor{keywordflow}{do} k = 1,count\_nonzero\_layers
129       densities(k) = densities(mapping(k))
130 \textcolor{keywordflow}{    enddo}
131 
132     \textcolor{comment}{! Based on source column density profile, interpolate to generate a new grid}
133     \textcolor{keyword}{call }build\_and\_interpolate\_grid(cs%interp\_CS, densities, count\_nonzero\_layers, &
134                                     h\_nv, xtmp, cs%target\_density, cs%nk, h\_new, &
135                                     x1, h\_neglect, h\_neglect\_edge)
136 
137     \textcolor{comment}{! Inflate vanished layers}
138     \textcolor{keyword}{call }old\_inflate\_layers\_1d(cs%min\_thickness, cs%nk, h\_new)
139 
140     \textcolor{comment}{! Comment: The following adjustment of h\_new, and re-calculation of h\_new via x1 needs to be removed}
141     x1(1) = 0.0 ; \textcolor{keywordflow}{do} k = 1,cs%nk ; x1(k+1) = x1(k) + h\_new(k) ;\textcolor{keywordflow}{ enddo}
142     \textcolor{keywordflow}{do} k = 1,cs%nk
143       h\_new(k) = x1(k+1) - x1(k)
144 \textcolor{keywordflow}{    enddo}
145 
146   \textcolor{keywordflow}{else} \textcolor{comment}{! count\_nonzero\_layers <= 1}
147     \textcolor{keywordflow}{if} (nz == cs%nk) \textcolor{keywordflow}{then}
148       h\_new(:) = h(:) \textcolor{comment}{! This keeps old behavior}
149     \textcolor{keywordflow}{else}
150       h\_new(:) = 0.
151       h\_new(1) = h(1)
152 \textcolor{keywordflow}{    endif}
153 \textcolor{keywordflow}{  endif}
154 
155   \textcolor{comment}{! Return interface positions}
156   \textcolor{keywordflow}{if} (cs%integrate\_downward\_for\_e) \textcolor{keywordflow}{then}
157     \textcolor{comment}{! Remapping is defined integrating from zero}
158     z\_interface(1) = 0.
159     \textcolor{keywordflow}{do} k = 1,cs%nk
160       z\_interface(k+1) = z\_interface(k) - h\_new(k)
161 \textcolor{keywordflow}{    enddo}
162   \textcolor{keywordflow}{else}
163     \textcolor{comment}{! The rest of the model defines grids integrating up from the bottom}
164     z\_interface(cs%nk+1) = -depth
165     \textcolor{keywordflow}{do} k = cs%nk,1,-1
166       z\_interface(k) = z\_interface(k+1) + h\_new(k)
167 \textcolor{keywordflow}{    enddo}
168 \textcolor{keywordflow}{  endif}
169 
\end{DoxyCode}
\mbox{\Hypertarget{namespacecoord__rho_ad79c2a471c8de0b16f0478813be7c29f}\label{namespacecoord__rho_ad79c2a471c8de0b16f0478813be7c29f}} 
\index{coord\+\_\+rho@{coord\+\_\+rho}!build\+\_\+rho\+\_\+column\+\_\+iteratively@{build\+\_\+rho\+\_\+column\+\_\+iteratively}}
\index{build\+\_\+rho\+\_\+column\+\_\+iteratively@{build\+\_\+rho\+\_\+column\+\_\+iteratively}!coord\+\_\+rho@{coord\+\_\+rho}}
\subsubsection{\texorpdfstring{build\+\_\+rho\+\_\+column\+\_\+iteratively()}{build\_rho\_column\_iteratively()}}
{\footnotesize\ttfamily subroutine coord\+\_\+rho\+::build\+\_\+rho\+\_\+column\+\_\+iteratively (\begin{DoxyParamCaption}\item[{type(\hyperlink{structcoord__rho_1_1rho__cs}{rho\+\_\+cs}), intent(in)}]{CS,  }\item[{type(remapping\+\_\+cs), intent(in)}]{remap\+CS,  }\item[{integer, intent(in)}]{nz,  }\item[{real, intent(in)}]{depth,  }\item[{real, dimension(nz), intent(in)}]{h,  }\item[{real, dimension(nz), intent(in)}]{T,  }\item[{real, dimension(nz), intent(in)}]{S,  }\item[{type(eos\+\_\+type), pointer}]{eqn\+\_\+of\+\_\+state,  }\item[{real, dimension(nz+1), intent(inout)}]{z\+Interface,  }\item[{real, intent(in), optional}]{h\+\_\+neglect,  }\item[{real, intent(in), optional}]{h\+\_\+neglect\+\_\+edge,  }\item[{real, intent(in), optional}]{dev\+\_\+tol }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Iteratively build a rho coordinate column. 

The algorithm operates as follows within each column\+:


\begin{DoxyEnumerate}
\item Given T \& S within each layer, the layer densities are computed.
\item Based on these layer densities, a global density profile is reconstructed (this profile is monotonically increasing and may be discontinuous)
\item The new grid interfaces are determined based on the target interface densities.
\item T \& S are remapped onto the new grid.
\item Return to step 1 until convergence or until the maximum number of iterations is reached, whichever comes first.
\end{DoxyEnumerate}


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em cs} & Regridding control structure\\
\hline
\mbox{\tt in}  & {\em remapcs} & Remapping parameters and options\\
\hline
\mbox{\tt in}  & {\em nz} & Number of levels\\
\hline
\mbox{\tt in}  & {\em depth} & Depth of ocean bottom \mbox{[}Z $\sim$$>$ m\mbox{]}\\
\hline
\mbox{\tt in}  & {\em h} & Layer thicknesses in Z coordinates \mbox{[}Z $\sim$$>$ m\mbox{]}\\
\hline
\mbox{\tt in}  & {\em t} & T for column \mbox{[}degC\mbox{]}\\
\hline
\mbox{\tt in}  & {\em s} & S for column \mbox{[}ppt\mbox{]}\\
\hline
 & {\em eqn\+\_\+of\+\_\+state} & Equation of state structure\\
\hline
\mbox{\tt in,out}  & {\em zinterface} & Absolute positions of interfaces\\
\hline
\mbox{\tt in}  & {\em h\+\_\+neglect} & A negligibly small width for the purpose of cell reconstructions in the same units as h \mbox{[}Z $\sim$$>$ m\mbox{]}\\
\hline
\mbox{\tt in}  & {\em h\+\_\+neglect\+\_\+edge} & A negligibly small width for the purpose of edge value calculations in the same units as h \mbox{[}Z $\sim$$>$ m\mbox{]}\\
\hline
\mbox{\tt in}  & {\em dev\+\_\+tol} & The tolerance for the deviation between successive grids for determining when the iterative solver has converged \mbox{[}Z $\sim$$>$ m\mbox{]} \\
\hline
\end{DoxyParams}


Definition at line 188 of file coord\+\_\+rho.\+F90.


\begin{DoxyCode}
188   \textcolor{keywordtype}{type}(rho\_cs),          \textcolor{keywordtype}{intent(in)}    :: cs\textcolor{comment}{ !< Regridding control structure}
189   \textcolor{keywordtype}{type}(remapping\_cs),    \textcolor{keywordtype}{intent(in)}    :: remapcs\textcolor{comment}{ !< Remapping parameters and options}
190   \textcolor{keywordtype}{integer},               \textcolor{keywordtype}{intent(in)}    :: nz\textcolor{comment}{ !< Number of levels}
191   \textcolor{keywordtype}{real},                  \textcolor{keywordtype}{intent(in)}    :: depth\textcolor{comment}{ !< Depth of ocean bottom [Z ~> m]}
192   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nz)},   \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{  !< Layer thicknesses in Z coordinates [Z ~> m]}
193   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nz)},   \textcolor{keywordtype}{intent(in)}    :: t\textcolor{comment}{  !< T for column [degC]}
194   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nz)},   \textcolor{keywordtype}{intent(in)}    :: s\textcolor{comment}{  !< S for column [ppt]}
195   \textcolor{keywordtype}{type}(eos\_type),        \textcolor{keywordtype}{pointer}       :: eqn\_of\_state\textcolor{comment}{ !< Equation of state structure}
196   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nz+1)}, \textcolor{keywordtype}{intent(inout)} :: zinterface\textcolor{comment}{ !< Absolute positions of interfaces}
197   \textcolor{keywordtype}{real},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: h\_neglect\textcolor{comment}{ !< A negligibly small width for the}
198 \textcolor{comment}{                                             !! purpose of cell reconstructions}
199 \textcolor{comment}{                                             !! in the same units as h [Z ~> m]}
200   \textcolor{keywordtype}{real},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: h\_neglect\_edge\textcolor{comment}{ !< A negligibly small width}
201 \textcolor{comment}{                                             !! for the purpose of edge value calculations}
202 \textcolor{comment}{                                             !! in the same units as h [Z ~> m]}
203   \textcolor{keywordtype}{real},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: dev\_tol\textcolor{comment}{ !< The tolerance for the deviation between}
204 \textcolor{comment}{                                             !! successive grids for determining when the}
205 \textcolor{comment}{                                             !! iterative solver has converged [Z ~> m]}
206 
207   \textcolor{comment}{! Local variables}
208   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nz+1)} :: x0, x1, xtmp \textcolor{comment}{! Temporary interface heights [Z ~> m]}
209   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nz)} :: pres       \textcolor{comment}{! The pressure used in the equation of state [R L2 T-2 ~> Pa].}
210   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nz)} :: densities  \textcolor{comment}{! Layer densities [R ~> kg m-3]}
211   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nz)} :: t\_tmp, s\_tmp \textcolor{comment}{! A temporary profile of temperature [degC] and salinity [ppt].}
212   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nz)} :: tmp        \textcolor{comment}{! A temporary variable holding a remapped variable.}
213   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nz)} :: h0, h1, htmp \textcolor{comment}{! Temporary thicknesses [Z ~> m]}
214   \textcolor{keywordtype}{real} :: deviation            \textcolor{comment}{! When iterating to determine the final grid, this is the}
215                                \textcolor{comment}{! deviation between two successive grids [Z ~> m].}
216   \textcolor{keywordtype}{real} :: deviation\_tol        \textcolor{comment}{! Deviation tolerance between succesive grids in}
217                                \textcolor{comment}{! regridding iterations [Z ~> m]}
218   \textcolor{keywordtype}{real} :: threshold            \textcolor{comment}{! The minimum thickness for a layer to be considered to exist [Z ~> m]}
219   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(nz)} :: mapping \textcolor{comment}{! The indices of the massive layers in the initial column.}
220   \textcolor{keywordtype}{integer} :: k, m, count\_nonzero\_layers
221 
222   \textcolor{comment}{!  Maximum number of regridding iterations}
223   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{parameter} :: nb\_regridding\_iterations = 1
224 
225   threshold = cs%min\_thickness
226   pres(:) = cs%ref\_pressure
227   t\_tmp(:) = t(:)
228   s\_tmp(:) = s(:)
229   h0(:) = h(:)
230 
231   \textcolor{comment}{! Start iterations to build grid}
232   m = 1
233   deviation\_tol = 1.0e-15*depth ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dev\_tol)) deviation\_tol = dev\_tol
234 
235   \textcolor{keywordflow}{do} m=1,nb\_regridding\_iterations
236 
237     \textcolor{comment}{! Construct column with vanished layers removed}
238     \textcolor{keyword}{call }copy\_finite\_thicknesses(nz, h0, threshold, count\_nonzero\_layers, htmp, mapping)
239     \textcolor{keywordflow}{if} ( count\_nonzero\_layers <= 1 ) \textcolor{keywordflow}{then}
240       h1(:) = h0(:)
241       \textcolor{keywordflow}{exit}  \textcolor{comment}{! stop iterations here}
242 \textcolor{keywordflow}{    endif}
243 
244     xtmp(1) = 0.0
245     \textcolor{keywordflow}{do} k = 1,count\_nonzero\_layers
246       xtmp(k+1) = xtmp(k) + htmp(k)
247 \textcolor{keywordflow}{    enddo}
248 
249     \textcolor{comment}{! Compute densities within current water column}
250     \textcolor{keyword}{call }calculate\_density( t\_tmp, s\_tmp, pres, densities, eqn\_of\_state)
251 
252     \textcolor{keywordflow}{do} k = 1,count\_nonzero\_layers
253       densities(k) = densities(mapping(k))
254 \textcolor{keywordflow}{    enddo}
255 
256     \textcolor{comment}{! One regridding iteration}
257     \textcolor{comment}{! Based on global density profile, interpolate to generate a new grid}
258     \textcolor{keyword}{call }build\_and\_interpolate\_grid(cs%interp\_CS, densities, count\_nonzero\_layers, &
259          htmp, xtmp, cs%target\_density, nz, h1, x1, h\_neglect, h\_neglect\_edge)
260 
261     \textcolor{keyword}{call }old\_inflate\_layers\_1d( cs%min\_thickness, nz, h1 )
262     x1(1) = 0.0 ; \textcolor{keywordflow}{do} k = 1,nz ; x1(k+1) = x1(k) + h1(k) ;\textcolor{keywordflow}{ enddo}
263 
264     \textcolor{comment}{! Remap T and S from previous grid to new grid}
265     \textcolor{keywordflow}{do} k = 1,nz
266       h1(k) = x1(k+1) - x1(k)
267 \textcolor{keywordflow}{    enddo}
268 
269     \textcolor{keyword}{call }remapping\_core\_h(remapcs, nz, h0, s, nz, h1, tmp, h\_neglect, h\_neglect\_edge)
270     s\_tmp(:) = tmp(:)
271 
272     \textcolor{keyword}{call }remapping\_core\_h(remapcs, nz, h0, t, nz, h1, tmp, h\_neglect, h\_neglect\_edge)
273     t\_tmp(:) = tmp(:)
274 
275     \textcolor{comment}{! Compute the deviation between two successive grids}
276     deviation = 0.0
277     x0(1) = 0.0
278     x1(1) = 0.0
279     \textcolor{keywordflow}{do} k = 2,nz
280       x0(k) = x0(k-1) + h0(k-1)
281       x1(k) = x1(k-1) + h1(k-1)
282       deviation = deviation + (x0(k)-x1(k))**2
283 \textcolor{keywordflow}{    enddo}
284     deviation = sqrt( deviation / (nz-1) )
285 
286     \textcolor{keywordflow}{if} ( deviation <= deviation\_tol ) \textcolor{keywordflow}{exit}
287 
288     \textcolor{comment}{! Copy final grid onto start grid for next iteration}
289     h0(:) = h1(:)
290 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! end regridding iterations}
291 
292   \textcolor{keywordflow}{if} (cs%integrate\_downward\_for\_e) \textcolor{keywordflow}{then}
293     zinterface(1) = 0.
294     \textcolor{keywordflow}{do} k = 1,nz
295       zinterface(k+1) = zinterface(k) - h1(k)
296       \textcolor{comment}{! Adjust interface position to accommodate inflating layers}
297       \textcolor{comment}{! without disturbing the interface above}
298 \textcolor{keywordflow}{    enddo}
299   \textcolor{keywordflow}{else}
300     \textcolor{comment}{! The rest of the model defines grids integrating up from the bottom}
301     zinterface(nz+1) = -depth
302     \textcolor{keywordflow}{do} k = nz,1,-1
303       zinterface(k) = zinterface(k+1) + h1(k)
304       \textcolor{comment}{! Adjust interface position to accommodate inflating layers}
305       \textcolor{comment}{! without disturbing the interface above}
306 \textcolor{keywordflow}{    enddo}
307 \textcolor{keywordflow}{  endif}
308 
\end{DoxyCode}
\mbox{\Hypertarget{namespacecoord__rho_a17064808dccea1f725fe82cd49cd2541}\label{namespacecoord__rho_a17064808dccea1f725fe82cd49cd2541}} 
\index{coord\+\_\+rho@{coord\+\_\+rho}!copy\+\_\+finite\+\_\+thicknesses@{copy\+\_\+finite\+\_\+thicknesses}}
\index{copy\+\_\+finite\+\_\+thicknesses@{copy\+\_\+finite\+\_\+thicknesses}!coord\+\_\+rho@{coord\+\_\+rho}}
\subsubsection{\texorpdfstring{copy\+\_\+finite\+\_\+thicknesses()}{copy\_finite\_thicknesses()}}
{\footnotesize\ttfamily subroutine coord\+\_\+rho\+::copy\+\_\+finite\+\_\+thicknesses (\begin{DoxyParamCaption}\item[{integer, intent(in)}]{nk,  }\item[{real, dimension(nk), intent(in)}]{h\+\_\+in,  }\item[{real, intent(in)}]{thresh,  }\item[{integer, intent(out)}]{nout,  }\item[{real, dimension(nk), intent(out)}]{h\+\_\+out,  }\item[{integer, dimension(nk), intent(out)}]{mapping }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Copy column thicknesses with vanished layers removed. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em nk} & Number of layer for h\+\_\+in, T\+\_\+in, S\+\_\+in\\
\hline
\mbox{\tt in}  & {\em h\+\_\+in} & Thickness of input column \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} or \mbox{[}Z $\sim$$>$ m\mbox{]}\\
\hline
\mbox{\tt in}  & {\em thresh} & Thickness threshold defining vanished layers \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} or \mbox{[}Z $\sim$$>$ m\mbox{]}\\
\hline
\mbox{\tt out}  & {\em nout} & Number of non-\/vanished layers\\
\hline
\mbox{\tt out}  & {\em h\+\_\+out} & Thickness of output column \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} or \mbox{[}Z $\sim$$>$ m\mbox{]}\\
\hline
\mbox{\tt out}  & {\em mapping} & Index of k-\/out corresponding to k-\/in \\
\hline
\end{DoxyParams}


Definition at line 313 of file coord\+\_\+rho.\+F90.


\begin{DoxyCode}
313   \textcolor{keywordtype}{integer},                \textcolor{keywordtype}{intent(in)}  :: nk\textcolor{comment}{      !< Number of layer for h\_in, T\_in, S\_in}
314   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)},    \textcolor{keywordtype}{intent(in)}  :: h\_in\textcolor{comment}{    !< Thickness of input column [H ~> m or kg m-2] or [Z ~>
       m]}
315   \textcolor{keywordtype}{real},                   \textcolor{keywordtype}{intent(in)}  :: thresh\textcolor{comment}{  !< Thickness threshold defining vanished}
316 \textcolor{comment}{                                                 !! layers [H ~> m or kg m-2] or [Z ~> m]}
317   \textcolor{keywordtype}{integer},                \textcolor{keywordtype}{intent(out)} :: nout\textcolor{comment}{    !< Number of non-vanished layers}
318   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)},    \textcolor{keywordtype}{intent(out)} :: h\_out\textcolor{comment}{   !< Thickness of output column [H ~> m or kg m-2] or [Z ~>
       m]}
319   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(nk)}, \textcolor{keywordtype}{intent(out)} :: mapping\textcolor{comment}{ !< Index of k-out corresponding to k-in}
320   \textcolor{comment}{! Local variables}
321   \textcolor{keywordtype}{integer} :: k, k\_thickest
322   \textcolor{keywordtype}{real} :: thickness\_in\_vanished \textcolor{comment}{! Summed thicknesses in discarded layers [H ~> m or kg m-2] or [Z ~> m]}
323   \textcolor{keywordtype}{real} :: thickest\_h\_out        \textcolor{comment}{! Thickness of the thickest layer [H ~> m or kg m-2] or [Z ~> m]}
324 
325   \textcolor{comment}{! Build up new grid}
326   nout = 0
327   thickness\_in\_vanished = 0.0
328   thickest\_h\_out = h\_in(1)
329   k\_thickest = 1
330   \textcolor{keywordflow}{do} k = 1, nk
331     mapping(k) = nout \textcolor{comment}{! Note k>=nout always}
332     h\_out(k) = 0.  \textcolor{comment}{! Make sure h\_out is set everywhere}
333     \textcolor{keywordflow}{if} (h\_in(k) > thresh) \textcolor{keywordflow}{then}
334       \textcolor{comment}{! For non-vanished layers}
335       nout = nout + 1
336       mapping(nout) = k
337       h\_out(nout) = h\_in(k)
338       \textcolor{keywordflow}{if} (h\_out(nout) > thickest\_h\_out) \textcolor{keywordflow}{then}
339         thickest\_h\_out = h\_out(nout)
340         k\_thickest = nout
341 \textcolor{keywordflow}{      endif}
342     \textcolor{keywordflow}{else}
343       \textcolor{comment}{! Add up mass in vanished layers}
344       thickness\_in\_vanished = thickness\_in\_vanished + h\_in(k)
345 \textcolor{keywordflow}{    endif}
346 \textcolor{keywordflow}{  enddo}
347 
348   \textcolor{comment}{! No finite layers}
349   \textcolor{keywordflow}{if} (nout <= 1) \textcolor{keywordflow}{return}
350 
351   \textcolor{comment}{! Adjust for any lost volume in vanished layers}
352   h\_out(k\_thickest) = h\_out(k\_thickest) + thickness\_in\_vanished
353 
\end{DoxyCode}
\mbox{\Hypertarget{namespacecoord__rho_a8e9c666d47d847ab8b64a22c470931d1}\label{namespacecoord__rho_a8e9c666d47d847ab8b64a22c470931d1}} 
\index{coord\+\_\+rho@{coord\+\_\+rho}!end\+\_\+coord\+\_\+rho@{end\+\_\+coord\+\_\+rho}}
\index{end\+\_\+coord\+\_\+rho@{end\+\_\+coord\+\_\+rho}!coord\+\_\+rho@{coord\+\_\+rho}}
\subsubsection{\texorpdfstring{end\+\_\+coord\+\_\+rho()}{end\_coord\_rho()}}
{\footnotesize\ttfamily subroutine, public coord\+\_\+rho\+::end\+\_\+coord\+\_\+rho (\begin{DoxyParamCaption}\item[{type(\hyperlink{structcoord__rho_1_1rho__cs}{rho\+\_\+cs}), pointer}]{CS }\end{DoxyParamCaption})}



This subroutine deallocates memory in the control structure for the \hyperlink{namespacecoord__rho}{coord\+\_\+rho} module. 


\begin{DoxyParams}{Parameters}
{\em cs} & Coordinate control structure \\
\hline
\end{DoxyParams}


Definition at line 61 of file coord\+\_\+rho.\+F90.


\begin{DoxyCode}
61   \textcolor{keywordtype}{type}(rho\_cs), \textcolor{keywordtype}{pointer} :: cs\textcolor{comment}{ !< Coordinate control structure}
62 
63   \textcolor{comment}{! nothing to do}
64   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{return}
65   \textcolor{keyword}{deallocate}(cs%target\_density)
66   \textcolor{keyword}{deallocate}(cs)
\end{DoxyCode}
\mbox{\Hypertarget{namespacecoord__rho_a86640b2be846b291c71f1c38b8ff9878}\label{namespacecoord__rho_a86640b2be846b291c71f1c38b8ff9878}} 
\index{coord\+\_\+rho@{coord\+\_\+rho}!init\+\_\+coord\+\_\+rho@{init\+\_\+coord\+\_\+rho}}
\index{init\+\_\+coord\+\_\+rho@{init\+\_\+coord\+\_\+rho}!coord\+\_\+rho@{coord\+\_\+rho}}
\subsubsection{\texorpdfstring{init\+\_\+coord\+\_\+rho()}{init\_coord\_rho()}}
{\footnotesize\ttfamily subroutine, public coord\+\_\+rho\+::init\+\_\+coord\+\_\+rho (\begin{DoxyParamCaption}\item[{type(\hyperlink{structcoord__rho_1_1rho__cs}{rho\+\_\+cs}), pointer}]{CS,  }\item[{integer, intent(in)}]{nk,  }\item[{real, intent(in)}]{ref\+\_\+pressure,  }\item[{real, dimension(\+:), intent(in)}]{target\+\_\+density,  }\item[{type(interp\+\_\+cs\+\_\+type), intent(in)}]{interp\+\_\+\+CS }\end{DoxyParamCaption})}



Initialise a rho\+\_\+\+CS with pointers to parameters. 


\begin{DoxyParams}[1]{Parameters}
 & {\em cs} & Unassociated pointer to hold the control structure\\
\hline
\mbox{\tt in}  & {\em nk} & Number of layers in the grid\\
\hline
\mbox{\tt in}  & {\em ref\+\_\+pressure} & Coordinate reference pressure \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]}\\
\hline
\mbox{\tt in}  & {\em target\+\_\+density} & Nominal density of interfaces \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}\\
\hline
\mbox{\tt in}  & {\em interp\+\_\+cs} & Controls for interpolation \\
\hline
\end{DoxyParams}


Definition at line 42 of file coord\+\_\+rho.\+F90.


\begin{DoxyCode}
42   \textcolor{keywordtype}{type}(rho\_cs),         \textcolor{keywordtype}{pointer}    :: cs\textcolor{comment}{ !< Unassociated pointer to hold the control structure}
43   \textcolor{keywordtype}{integer},              \textcolor{keywordtype}{intent(in)} :: nk\textcolor{comment}{ !< Number of layers in the grid}
44   \textcolor{keywordtype}{real},                 \textcolor{keywordtype}{intent(in)} :: ref\_pressure\textcolor{comment}{ !< Coordinate reference pressure [R L2 T-2 ~> Pa]}
45   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:)},   \textcolor{keywordtype}{intent(in)} :: target\_density\textcolor{comment}{ !< Nominal density of interfaces [R ~> kg m-3]}
46   \textcolor{keywordtype}{type}(interp\_cs\_type), \textcolor{keywordtype}{intent(in)} :: interp\_cs\textcolor{comment}{ !< Controls for interpolation}
47 
48   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"init\_coord\_rho: CS already associated!"})
49   \textcolor{keyword}{allocate}(cs)
50   \textcolor{keyword}{allocate}(cs%target\_density(nk+1))
51 
52   cs%nk                = nk
53   cs%ref\_pressure      = ref\_pressure
54   cs%target\_density(:) = target\_density(:)
55   cs%interp\_CS         = interp\_cs
56 
\end{DoxyCode}
\mbox{\Hypertarget{namespacecoord__rho_afbc86f29df8e39eb793d12075d018518}\label{namespacecoord__rho_afbc86f29df8e39eb793d12075d018518}} 
\index{coord\+\_\+rho@{coord\+\_\+rho}!old\+\_\+inflate\+\_\+layers\+\_\+1d@{old\+\_\+inflate\+\_\+layers\+\_\+1d}}
\index{old\+\_\+inflate\+\_\+layers\+\_\+1d@{old\+\_\+inflate\+\_\+layers\+\_\+1d}!coord\+\_\+rho@{coord\+\_\+rho}}
\subsubsection{\texorpdfstring{old\+\_\+inflate\+\_\+layers\+\_\+1d()}{old\_inflate\_layers\_1d()}}
{\footnotesize\ttfamily subroutine, public coord\+\_\+rho\+::old\+\_\+inflate\+\_\+layers\+\_\+1d (\begin{DoxyParamCaption}\item[{real, intent(in)}]{min\+\_\+thickness,  }\item[{integer, intent(in)}]{nk,  }\item[{real, dimension(\+:), intent(inout)}]{h }\end{DoxyParamCaption})}



Inflate vanished layers to finite (nonzero) width. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em min\+\_\+thickness} & Minimum allowed thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em nk} & Number of layers in the grid\\
\hline
\mbox{\tt in,out}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\end{DoxyParams}


Definition at line 359 of file coord\+\_\+rho.\+F90.


\begin{DoxyCode}
359 
360   \textcolor{comment}{! Argument}
361   \textcolor{keywordtype}{real},               \textcolor{keywordtype}{intent(in)}    :: min\_thickness\textcolor{comment}{ !< Minimum allowed thickness [H ~> m or kg m-2]}
362   \textcolor{keywordtype}{integer},            \textcolor{keywordtype}{intent(in)}    :: nk\textcolor{comment}{  !< Number of layers in the grid}
363   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:)}, \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{   !< Layer thicknesses [H ~> m or kg m-2]}
364 
365   \textcolor{comment}{! Local variable}
366   \textcolor{keywordtype}{integer}   :: k
367   \textcolor{keywordtype}{integer}   :: k\_found
368   \textcolor{keywordtype}{integer}   :: count\_nonzero\_layers
369   \textcolor{keywordtype}{real}      :: delta
370   \textcolor{keywordtype}{real}      :: correction
371   \textcolor{keywordtype}{real}      :: maxthickness
372 
373   \textcolor{comment}{! Count number of nonzero layers}
374   count\_nonzero\_layers = 0
375   \textcolor{keywordflow}{do} k = 1,nk
376     \textcolor{keywordflow}{if} ( h(k) > min\_thickness ) \textcolor{keywordflow}{then}
377       count\_nonzero\_layers = count\_nonzero\_layers + 1
378 \textcolor{keywordflow}{    endif}
379 \textcolor{keywordflow}{  enddo}
380 
381   \textcolor{comment}{! If all layer thicknesses are greater than the threshold, exit routine}
382   \textcolor{keywordflow}{if} ( count\_nonzero\_layers == nk ) \textcolor{keywordflow}{return}
383 
384   \textcolor{comment}{! If all thicknesses are zero, inflate them all and exit}
385   \textcolor{keywordflow}{if} ( count\_nonzero\_layers == 0 ) \textcolor{keywordflow}{then}
386     \textcolor{keywordflow}{do} k = 1,nk
387       h(k) = min\_thickness
388 \textcolor{keywordflow}{    enddo}
389     \textcolor{keywordflow}{return}
390 \textcolor{keywordflow}{  endif}
391 
392   \textcolor{comment}{! Inflate zero layers}
393   correction = 0.0
394   \textcolor{keywordflow}{do} k = 1,nk
395     \textcolor{keywordflow}{if} ( h(k) <= min\_thickness ) \textcolor{keywordflow}{then}
396       delta = min\_thickness - h(k)
397       correction = correction + delta
398       h(k) = h(k) + delta
399 \textcolor{keywordflow}{    endif}
400 \textcolor{keywordflow}{  enddo}
401 
402   \textcolor{comment}{! Modify thicknesses of nonzero layers to ensure volume conservation}
403   maxthickness = h(1)
404   k\_found = 1
405   \textcolor{keywordflow}{do} k = 1,nk
406     \textcolor{keywordflow}{if} ( h(k) > maxthickness ) \textcolor{keywordflow}{then}
407       maxthickness = h(k)
408       k\_found = k
409 \textcolor{keywordflow}{    endif}
410 \textcolor{keywordflow}{  enddo}
411 
412   h(k\_found) = h(k\_found) - correction
413 
\end{DoxyCode}
\mbox{\Hypertarget{namespacecoord__rho_a6b2a48523247f59c7b4b5b7941334c25}\label{namespacecoord__rho_a6b2a48523247f59c7b4b5b7941334c25}} 
\index{coord\+\_\+rho@{coord\+\_\+rho}!set\+\_\+rho\+\_\+params@{set\+\_\+rho\+\_\+params}}
\index{set\+\_\+rho\+\_\+params@{set\+\_\+rho\+\_\+params}!coord\+\_\+rho@{coord\+\_\+rho}}
\subsubsection{\texorpdfstring{set\+\_\+rho\+\_\+params()}{set\_rho\_params()}}
{\footnotesize\ttfamily subroutine, public coord\+\_\+rho\+::set\+\_\+rho\+\_\+params (\begin{DoxyParamCaption}\item[{type(\hyperlink{structcoord__rho_1_1rho__cs}{rho\+\_\+cs}), pointer}]{CS,  }\item[{real, intent(in), optional}]{min\+\_\+thickness,  }\item[{logical, intent(in), optional}]{integrate\+\_\+downward\+\_\+for\+\_\+e,  }\item[{type(interp\+\_\+cs\+\_\+type), intent(in), optional}]{interp\+\_\+\+CS }\end{DoxyParamCaption})}



This subroutine can be used to set the parameters for the \hyperlink{namespacecoord__rho}{coord\+\_\+rho} module. 


\begin{DoxyParams}[1]{Parameters}
 & {\em cs} & Coordinate control structure\\
\hline
\mbox{\tt in}  & {\em min\+\_\+thickness} & Minimum allowed thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em integrate\+\_\+downward\+\_\+for\+\_\+e} & If true, integrate for interface positions from the top downward. If false, integrate from the bottom upward, as does the rest of the model.\\
\hline
\mbox{\tt in}  & {\em interp\+\_\+cs} & Controls for interpolation \\
\hline
\end{DoxyParams}


Definition at line 71 of file coord\+\_\+rho.\+F90.


\begin{DoxyCode}
71   \textcolor{keywordtype}{type}(rho\_cs),      \textcolor{keywordtype}{pointer}    :: cs\textcolor{comment}{ !< Coordinate control structure}
72   \textcolor{keywordtype}{real},    \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)} :: min\_thickness\textcolor{comment}{ !< Minimum allowed thickness [H ~> m or kg m-2]}
73   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)} :: integrate\_downward\_for\_e\textcolor{comment}{ !< If true, integrate for interface}
74 \textcolor{comment}{                                      !! positions from the top downward.  If false, integrate}
75 \textcolor{comment}{                                      !! from the bottom upward, as does the rest of the model.}
76 
77   \textcolor{keywordtype}{type}(interp\_cs\_type), \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)} :: interp\_cs\textcolor{comment}{ !< Controls for interpolation}
78 
79   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(cs)) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"set\_rho\_params: CS not associated"})
80 
81   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(min\_thickness)) cs%min\_thickness = min\_thickness
82   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(integrate\_downward\_for\_e)) cs%integrate\_downward\_for\_e = integrate\_downward\_for\_e
83   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(interp\_cs)) cs%interp\_CS = interp\_cs
\end{DoxyCode}
