\hypertarget{namespacemom__random}{}\doxysection{mom\+\_\+random Module Reference}
\label{namespacemom__random}\index{mom\_random@{mom\_random}}


\doxysubsection{Detailed Description}
Provides gridded random number capability. 

Provides M\+O\+M6 wrappers to the F\+MS implementation of the Mersenne twister.

Example usage\+: 
\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keywordtype}{type}(PRNG) :: rng}
\DoxyCodeLine{\textcolor{keywordtype}{real} :: rn}
\DoxyCodeLine{\textcolor{keyword}{call }random\_0d\_constructor(rng, time, seed) \textcolor{comment}{! Call this each time-\/step}}
\DoxyCodeLine{rn = random\_01(rng)}
\DoxyCodeLine{rn = random\_norm(rng)}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{type}(PRNG) :: rng}
\DoxyCodeLine{\textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:,:)} :: rn2d}
\DoxyCodeLine{\textcolor{keyword}{call }random\_2d\_constructor(rng, hi, time, seed) \textcolor{comment}{! Call this each time-\/step}}
\DoxyCodeLine{\textcolor{keyword}{call }random\_2d\_01(rng, hi, rn2d)}
\DoxyCodeLine{\textcolor{keyword}{call }random\_2d\_norm(rng, hi, rn2d)}
\DoxyCodeLine{}
\DoxyCodeLine{note: reproducibility across restarts is implemented by using time-\/derived}
\DoxyCodeLine{seeds to pass to the mersenne twister. it is therefore important that any}
\DoxyCodeLine{prng \textcolor{keyword}{type} be re-\/initialized each time-\/step.}
\end{DoxyCode}
 \doxysubsection*{Data Types}
\begin{DoxyCompactItemize}
\item 
type \mbox{\hyperlink{structmom__random_1_1prng}{prng}}
\begin{DoxyCompactList}\small\item\em Container for pseudo-\/random number generators. \end{DoxyCompactList}\end{DoxyCompactItemize}
\doxysubsection*{Functions/\+Subroutines}
\begin{DoxyCompactItemize}
\item 
real function, public \mbox{\hyperlink{namespacemom__random_a4a2b88d7b2768f18897efac819791bc7}{random\+\_\+01}} (CS)
\begin{DoxyCompactList}\small\item\em Returns a random number between 0 and 1. \end{DoxyCompactList}\item 
real function, public \mbox{\hyperlink{namespacemom__random_a9eac722032e06d49179558722d133cc6}{random\+\_\+norm}} (CS)
\begin{DoxyCompactList}\small\item\em Returns an approximately normally distributed random number with mean 0 and variance 1. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__random_ad3a0c1bdadf5248234717987e6733bd0}{random\+\_\+2d\+\_\+01}} (CS, HI, rand)
\begin{DoxyCompactList}\small\item\em Generates random numbers between 0 and 1 for each cell of the model grid. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__random_a3a029841b8ea26fc4cbd182a83700c39}{random\+\_\+2d\+\_\+norm}} (CS, HI, rand)
\begin{DoxyCompactList}\small\item\em Returns an approximately normally distributed random number with mean 0 and variance 1 for each cell of the model grid. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__random_ad6fd0718a45b6896bbb40a58b7ca3499}{random\+\_\+0d\+\_\+constructor}} (CS, Time, seed)
\begin{DoxyCompactList}\small\item\em Constructor for scalar P\+R\+NG. Can be used to reset the sequence. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__random_ae69c41901903bc31dccf0d4fa122ae9c}{random\+\_\+2d\+\_\+constructor}} (CS, HI, Time, seed)
\begin{DoxyCompactList}\small\item\em Constructor for gridded P\+R\+NG. Can be used to reset the sequence. \end{DoxyCompactList}\item 
integer function \mbox{\hyperlink{namespacemom__random_a48fc0d0eab640dedd565d14603e5b85c}{seed\+\_\+from\+\_\+time}} (Time)
\begin{DoxyCompactList}\small\item\em Return a seed derived as hash of values in Time. \end{DoxyCompactList}\item 
integer function \mbox{\hyperlink{namespacemom__random_a8605064631f07f3a7e9704422732c81c}{seed\+\_\+from\+\_\+index}} (HI, i, j)
\begin{DoxyCompactList}\small\item\em Create seed from position index. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__random_abf96abc7e4ff0bd1a5a9016127669197}{random\+\_\+destruct}} (CS)
\begin{DoxyCompactList}\small\item\em Destructor for P\+R\+NG. \end{DoxyCompactList}\item 
logical function, public \mbox{\hyperlink{namespacemom__random_a9c1ce606b5ca91a6a550302aa5a552ae}{random\+\_\+unit\+\_\+tests}} (verbose)
\begin{DoxyCompactList}\small\item\em Runs some statistical tests on the P\+R\+NG. \end{DoxyCompactList}\item 
logical function \mbox{\hyperlink{namespacemom__random_a32dd7680a649486b9b96efc7c21ee7a1}{test\+\_\+fn}} (verbose, good, label, rvalue, ivalue)
\begin{DoxyCompactList}\small\item\em Convenience function for reporting result of test. \end{DoxyCompactList}\end{DoxyCompactItemize}


\doxysubsection{Function/\+Subroutine Documentation}
\mbox{\Hypertarget{namespacemom__random_a4a2b88d7b2768f18897efac819791bc7}\label{namespacemom__random_a4a2b88d7b2768f18897efac819791bc7}} 
\index{mom\_random@{mom\_random}!random\_01@{random\_01}}
\index{random\_01@{random\_01}!mom\_random@{mom\_random}}
\doxysubsubsection{\texorpdfstring{random\_01()}{random\_01()}}
{\footnotesize\ttfamily real function, public mom\+\_\+random\+::random\+\_\+01 (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__random_1_1prng}{prng}}), intent(inout)}]{CS }\end{DoxyParamCaption})}



Returns a random number between 0 and 1. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in,out}}  & {\em cs} & Container for pseudo-\/random number generators \\
\hline
\end{DoxyParams}


Definition at line 43 of file M\+O\+M\+\_\+random.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{43   \textcolor{keywordtype}{type}(PRNG), \textcolor{keywordtype}{intent(inout)} :: CS\textcolor{comment}{ !< Container for pseudo-\/random number generators}}
\DoxyCodeLine{44 }
\DoxyCodeLine{45   random\_01 = getrandomreal(cs\%stream0d)}
\DoxyCodeLine{46 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__random_ad6fd0718a45b6896bbb40a58b7ca3499}\label{namespacemom__random_ad6fd0718a45b6896bbb40a58b7ca3499}} 
\index{mom\_random@{mom\_random}!random\_0d\_constructor@{random\_0d\_constructor}}
\index{random\_0d\_constructor@{random\_0d\_constructor}!mom\_random@{mom\_random}}
\doxysubsubsection{\texorpdfstring{random\_0d\_constructor()}{random\_0d\_constructor()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+random\+::random\+\_\+0d\+\_\+constructor (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__random_1_1prng}{prng}}), intent(inout)}]{CS,  }\item[{type(time\+\_\+type), intent(in)}]{Time,  }\item[{integer, intent(in)}]{seed }\end{DoxyParamCaption})}



Constructor for scalar P\+R\+NG. Can be used to reset the sequence. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in,out}}  & {\em cs} & Container for pseudo-\/random number generators \\
\hline
\mbox{\texttt{ in}}  & {\em time} & Current model time \\
\hline
\mbox{\texttt{ in}}  & {\em seed} & Seed for P\+R\+NG \\
\hline
\end{DoxyParams}


Definition at line 102 of file M\+O\+M\+\_\+random.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{102   \textcolor{keywordtype}{type}(PRNG),      \textcolor{keywordtype}{intent(inout)} :: CS\textcolor{comment}{   !< Container for pseudo-\/random number generators}}
\DoxyCodeLine{103   \textcolor{keywordtype}{type}(time\_type), \textcolor{keywordtype}{intent(in)}    :: Time\textcolor{comment}{ !< Current model time}}
\DoxyCodeLine{104   \textcolor{keywordtype}{integer},         \textcolor{keywordtype}{intent(in)}    :: seed\textcolor{comment}{ !< Seed for PRNG}}
\DoxyCodeLine{105   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{106   \textcolor{keywordtype}{integer} :: tseed}
\DoxyCodeLine{107 }
\DoxyCodeLine{108   tseed = seed\_from\_time(time)}
\DoxyCodeLine{109   tseed = ieor(tseed, seed)}
\DoxyCodeLine{110   cs\%stream0d = new\_randomnumbersequence(tseed)}
\DoxyCodeLine{111 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__random_ad3a0c1bdadf5248234717987e6733bd0}\label{namespacemom__random_ad3a0c1bdadf5248234717987e6733bd0}} 
\index{mom\_random@{mom\_random}!random\_2d\_01@{random\_2d\_01}}
\index{random\_2d\_01@{random\_2d\_01}!mom\_random@{mom\_random}}
\doxysubsubsection{\texorpdfstring{random\_2d\_01()}{random\_2d\_01()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+random\+::random\+\_\+2d\+\_\+01 (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__random_1_1prng}{prng}}), intent(inout)}]{CS,  }\item[{type(hor\+\_\+index\+\_\+type), intent(in)}]{HI,  }\item[{real, dimension( hi \%isd\+: hi \%ied, hi \%jsd\+: hi \%jed), intent(out)}]{rand }\end{DoxyParamCaption})}



Generates random numbers between 0 and 1 for each cell of the model grid. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in,out}}  & {\em cs} & Container for pseudo-\/random number generators \\
\hline
\mbox{\texttt{ in}}  & {\em hi} & Horizontal index structure \\
\hline
\mbox{\texttt{ out}}  & {\em rand} & Random numbers between 0 and 1 \\
\hline
\end{DoxyParams}


Definition at line 64 of file M\+O\+M\+\_\+random.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{64   \textcolor{keywordtype}{type}(PRNG),           \textcolor{keywordtype}{intent(inout)} :: CS\textcolor{comment}{ !< Container for pseudo-\/random number generators}}
\DoxyCodeLine{65   \textcolor{keywordtype}{type}(hor\_index\_type), \textcolor{keywordtype}{intent(in)}    :: HI\textcolor{comment}{ !< Horizontal index structure}}
\DoxyCodeLine{66 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(HI),SZJ\_(HI))}, \textcolor{keywordtype}{intent(out)} :: rand\textcolor{comment}{ !< Random numbers between 0 and 1}}
\DoxyCodeLine{67   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{68   \textcolor{keywordtype}{integer} :: i,j}
\DoxyCodeLine{69 }
\DoxyCodeLine{70   \textcolor{keywordflow}{do} j = hi\%jsd,hi\%jed}
\DoxyCodeLine{71     \textcolor{keywordflow}{do} i = hi\%isd,hi\%ied}
\DoxyCodeLine{72       rand(i,j) = getrandomreal( cs\%stream2d(i,j) )}
\DoxyCodeLine{73 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{74 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{75 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__random_ae69c41901903bc31dccf0d4fa122ae9c}\label{namespacemom__random_ae69c41901903bc31dccf0d4fa122ae9c}} 
\index{mom\_random@{mom\_random}!random\_2d\_constructor@{random\_2d\_constructor}}
\index{random\_2d\_constructor@{random\_2d\_constructor}!mom\_random@{mom\_random}}
\doxysubsubsection{\texorpdfstring{random\_2d\_constructor()}{random\_2d\_constructor()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+random\+::random\+\_\+2d\+\_\+constructor (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__random_1_1prng}{prng}}), intent(inout)}]{CS,  }\item[{type(hor\+\_\+index\+\_\+type), intent(in)}]{HI,  }\item[{type(time\+\_\+type), intent(in)}]{Time,  }\item[{integer, intent(in)}]{seed }\end{DoxyParamCaption})}



Constructor for gridded P\+R\+NG. Can be used to reset the sequence. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in,out}}  & {\em cs} & Container for pseudo-\/random number generators \\
\hline
\mbox{\texttt{ in}}  & {\em hi} & Horizontal index structure \\
\hline
\mbox{\texttt{ in}}  & {\em time} & Current model time \\
\hline
\mbox{\texttt{ in}}  & {\em seed} & Seed for P\+R\+NG \\
\hline
\end{DoxyParams}


Definition at line 116 of file M\+O\+M\+\_\+random.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{116   \textcolor{keywordtype}{type}(PRNG),           \textcolor{keywordtype}{intent(inout)} :: CS\textcolor{comment}{   !< Container for pseudo-\/random number generators}}
\DoxyCodeLine{117   \textcolor{keywordtype}{type}(hor\_index\_type), \textcolor{keywordtype}{intent(in)}    :: HI\textcolor{comment}{   !< Horizontal index structure}}
\DoxyCodeLine{118   \textcolor{keywordtype}{type}(time\_type),      \textcolor{keywordtype}{intent(in)}    :: Time\textcolor{comment}{ !< Current model time}}
\DoxyCodeLine{119   \textcolor{keywordtype}{integer},              \textcolor{keywordtype}{intent(in)}    :: seed\textcolor{comment}{ !< Seed for PRNG}}
\DoxyCodeLine{120   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{121   \textcolor{keywordtype}{integer} :: i,j,sseed,tseed}
\DoxyCodeLine{122 }
\DoxyCodeLine{123   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{allocated}(cs\%stream2d)) \textcolor{keyword}{allocate}( cs\%stream2d(hi\%isd:hi\%ied,hi\%jsd:hi\%jed) )}
\DoxyCodeLine{124 }
\DoxyCodeLine{125   tseed = seed\_from\_time(time)}
\DoxyCodeLine{126   tseed = ieor(tseed*9007, seed)}
\DoxyCodeLine{127   \textcolor{keywordflow}{do} j = hi\%jsd,hi\%jed}
\DoxyCodeLine{128     \textcolor{keywordflow}{do} i = hi\%isd,hi\%ied}
\DoxyCodeLine{129       sseed = seed\_from\_index(hi, i, j)}
\DoxyCodeLine{130       sseed = ieor(tseed, sseed*7993)}
\DoxyCodeLine{131       cs\%stream2d(i,j) = new\_randomnumbersequence(sseed)}
\DoxyCodeLine{132 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{133 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{134 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__random_a3a029841b8ea26fc4cbd182a83700c39}\label{namespacemom__random_a3a029841b8ea26fc4cbd182a83700c39}} 
\index{mom\_random@{mom\_random}!random\_2d\_norm@{random\_2d\_norm}}
\index{random\_2d\_norm@{random\_2d\_norm}!mom\_random@{mom\_random}}
\doxysubsubsection{\texorpdfstring{random\_2d\_norm()}{random\_2d\_norm()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+random\+::random\+\_\+2d\+\_\+norm (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__random_1_1prng}{prng}}), intent(inout)}]{CS,  }\item[{type(hor\+\_\+index\+\_\+type), intent(in)}]{HI,  }\item[{real, dimension( hi \%isd\+: hi \%ied, hi \%jsd\+: hi \%jed), intent(out)}]{rand }\end{DoxyParamCaption})}



Returns an approximately normally distributed random number with mean 0 and variance 1 for each cell of the model grid. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in,out}}  & {\em cs} & Container for pseudo-\/random number generators \\
\hline
\mbox{\texttt{ in}}  & {\em hi} & Horizontal index structure \\
\hline
\mbox{\texttt{ out}}  & {\em rand} & Random numbers between 0 and 1 \\
\hline
\end{DoxyParams}


Definition at line 81 of file M\+O\+M\+\_\+random.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{81   \textcolor{keywordtype}{type}(PRNG),           \textcolor{keywordtype}{intent(inout)} :: CS\textcolor{comment}{ !< Container for pseudo-\/random number generators}}
\DoxyCodeLine{82   \textcolor{keywordtype}{type}(hor\_index\_type), \textcolor{keywordtype}{intent(in)}    :: HI\textcolor{comment}{ !< Horizontal index structure}}
\DoxyCodeLine{83 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(HI),SZJ\_(HI))}, \textcolor{keywordtype}{intent(out)} :: rand\textcolor{comment}{ !< Random numbers between 0 and 1}}
\DoxyCodeLine{84   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{85   \textcolor{keywordtype}{integer} :: i,j,n}
\DoxyCodeLine{86 }
\DoxyCodeLine{87   \textcolor{keywordflow}{do} j = hi\%jsd,hi\%jed}
\DoxyCodeLine{88     \textcolor{keywordflow}{do} i = hi\%isd,hi\%ied}
\DoxyCodeLine{89       rand(i,j) = getrandomreal( cs\%stream2d(i,j) ) -\/ 0.5}
\DoxyCodeLine{90 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{91     \textcolor{keywordflow}{do} n = 1,11}
\DoxyCodeLine{92       \textcolor{keywordflow}{do} i = hi\%isd,hi\%ied}
\DoxyCodeLine{93         rand(i,j) = rand(i,j) + ( getrandomreal( cs\%stream2d(i,j) ) -\/ 0.5 )}
\DoxyCodeLine{94 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{95 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{96 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{97 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__random_abf96abc7e4ff0bd1a5a9016127669197}\label{namespacemom__random_abf96abc7e4ff0bd1a5a9016127669197}} 
\index{mom\_random@{mom\_random}!random\_destruct@{random\_destruct}}
\index{random\_destruct@{random\_destruct}!mom\_random@{mom\_random}}
\doxysubsubsection{\texorpdfstring{random\_destruct()}{random\_destruct()}}
{\footnotesize\ttfamily subroutine mom\+\_\+random\+::random\+\_\+destruct (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__random_1_1prng}{prng}}), pointer}]{CS }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Destructor for P\+R\+NG. 


\begin{DoxyParams}{Parameters}
{\em cs} & Container for pseudo-\/random number generators \\
\hline
\end{DoxyParams}


Definition at line 176 of file M\+O\+M\+\_\+random.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{176   \textcolor{keywordtype}{type}(PRNG), \textcolor{keywordtype}{pointer} :: CS\textcolor{comment}{ !< Container for pseudo-\/random number generators}}
\DoxyCodeLine{177 }
\DoxyCodeLine{178   \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs\%stream2d)) \textcolor{keyword}{deallocate}(cs\%stream2d)}
\DoxyCodeLine{179   \textcolor{comment}{!deallocate(CS)}}

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__random_a9eac722032e06d49179558722d133cc6}\label{namespacemom__random_a9eac722032e06d49179558722d133cc6}} 
\index{mom\_random@{mom\_random}!random\_norm@{random\_norm}}
\index{random\_norm@{random\_norm}!mom\_random@{mom\_random}}
\doxysubsubsection{\texorpdfstring{random\_norm()}{random\_norm()}}
{\footnotesize\ttfamily real function, public mom\+\_\+random\+::random\+\_\+norm (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__random_1_1prng}{prng}}), intent(inout)}]{CS }\end{DoxyParamCaption})}



Returns an approximately normally distributed random number with mean 0 and variance 1. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in,out}}  & {\em cs} & Container for pseudo-\/random number generators \\
\hline
\end{DoxyParams}


Definition at line 51 of file M\+O\+M\+\_\+random.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{51   \textcolor{keywordtype}{type}(PRNG), \textcolor{keywordtype}{intent(inout)} :: CS\textcolor{comment}{ !< Container for pseudo-\/random number generators}}
\DoxyCodeLine{52   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{53   \textcolor{keywordtype}{integer} :: i}
\DoxyCodeLine{54 }
\DoxyCodeLine{55   random\_norm = getrandomreal(cs\%stream0d) -\/ 0.5}
\DoxyCodeLine{56   \textcolor{keywordflow}{do} i = 1,11}
\DoxyCodeLine{57     random\_norm = random\_norm + ( getrandomreal(cs\%stream0d) -\/ 0.5 )}
\DoxyCodeLine{58 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{59 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__random_a9c1ce606b5ca91a6a550302aa5a552ae}\label{namespacemom__random_a9c1ce606b5ca91a6a550302aa5a552ae}} 
\index{mom\_random@{mom\_random}!random\_unit\_tests@{random\_unit\_tests}}
\index{random\_unit\_tests@{random\_unit\_tests}!mom\_random@{mom\_random}}
\doxysubsubsection{\texorpdfstring{random\_unit\_tests()}{random\_unit\_tests()}}
{\footnotesize\ttfamily logical function, public mom\+\_\+random\+::random\+\_\+unit\+\_\+tests (\begin{DoxyParamCaption}\item[{logical}]{verbose }\end{DoxyParamCaption})}



Runs some statistical tests on the P\+R\+NG. 


\begin{DoxyParams}{Parameters}
{\em verbose} & True if results should be written to stdout \\
\hline
\end{DoxyParams}


Definition at line 184 of file M\+O\+M\+\_\+random.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{184   \textcolor{keywordtype}{logical} :: verbose\textcolor{comment}{ !< True if results should be written to stdout}}
\DoxyCodeLine{185   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{186   \textcolor{keywordtype}{type}(PRNG) :: test\_rng \textcolor{comment}{! Generator}}
\DoxyCodeLine{187   \textcolor{keywordtype}{type}(time\_type) :: Time \textcolor{comment}{! Model time}}
\DoxyCodeLine{188 \textcolor{keywordtype}{  real} :: r1, r2, r3 \textcolor{comment}{! Some random numbers and re-\/used work variables}}
\DoxyCodeLine{189 \textcolor{keywordtype}{  real} :: mean, var, ar1, std \textcolor{comment}{! Some statistics}}
\DoxyCodeLine{190   \textcolor{keywordtype}{integer} :: stdunit \textcolor{comment}{! For messages}}
\DoxyCodeLine{191   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{parameter} :: n\_samples = 800}
\DoxyCodeLine{192   \textcolor{keywordtype}{integer} :: i, j, ni, nj}
\DoxyCodeLine{193   \textcolor{comment}{! Fake being on a decomposed domain}}
\DoxyCodeLine{194   \textcolor{keywordtype}{type}(hor\_index\_type), \textcolor{keywordtype}{pointer} :: HI => null() \textcolor{comment}{!< Not the real HI}}
\DoxyCodeLine{195 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(:,:)}, \textcolor{keywordtype}{allocatable} :: r2d \textcolor{comment}{! Random numbers}}
\DoxyCodeLine{196 }
\DoxyCodeLine{197   \textcolor{comment}{! Fake a decomposed domain}}
\DoxyCodeLine{198   ni = 6}
\DoxyCodeLine{199   nj = 9}
\DoxyCodeLine{200   \textcolor{keyword}{allocate}(hi)}
\DoxyCodeLine{201   hi\%isd = 0}
\DoxyCodeLine{202   hi\%ied = ni+1}
\DoxyCodeLine{203   hi\%jsd = 0}
\DoxyCodeLine{204   hi\%jed = nj+1}
\DoxyCodeLine{205   hi\%niglobal = ni}
\DoxyCodeLine{206   hi\%njglobal = nj}
\DoxyCodeLine{207   hi\%idg\_offset = 0}
\DoxyCodeLine{208   hi\%jdg\_offset = 0}
\DoxyCodeLine{209 }
\DoxyCodeLine{210   random\_unit\_tests = .false.}
\DoxyCodeLine{211   stdunit = stdout}
\DoxyCodeLine{212   \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,a)'}) \textcolor{stringliteral}{'==== MOM\_random: random\_unit\_tests ======================='}}
\DoxyCodeLine{213 }
\DoxyCodeLine{214   \textcolor{keywordflow}{if} (verbose) \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random: ",a)'}) \textcolor{stringliteral}{'-\/-\/ Time-\/based seeds -\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/'}}
\DoxyCodeLine{215   \textcolor{comment}{! Check time-\/based seed generation}}
\DoxyCodeLine{216   time = set\_date(1903, 11, 21, 13, 47, 29)}
\DoxyCodeLine{217   i = seed\_from\_time(time)}
\DoxyCodeLine{218   random\_unit\_tests = random\_unit\_tests .or. \&}
\DoxyCodeLine{219       test\_fn(verbose, i==212584341, \textcolor{stringliteral}{'time seed 1903/11/21 13:47:29'}, ivalue=i)}
\DoxyCodeLine{220   time = set\_date(1903, 11, 22, 13, 47, 29)}
\DoxyCodeLine{221   i = seed\_from\_time(time)}
\DoxyCodeLine{222   random\_unit\_tests = random\_unit\_tests .or.\&}
\DoxyCodeLine{223        test\_fn(verbose, i==212584342, \textcolor{stringliteral}{'time seed 1903/11/22 13:47:29'}, ivalue=i)}
\DoxyCodeLine{224   time = set\_date(1903, 11, 21, 13, 47, 30)}
\DoxyCodeLine{225   i = seed\_from\_time(time)}
\DoxyCodeLine{226   random\_unit\_tests = random\_unit\_tests .or.\&}
\DoxyCodeLine{227        test\_fn(verbose, i==212596634, \textcolor{stringliteral}{'time seed 1903/11/21 13:47:30'}, ivalue=i)}
\DoxyCodeLine{228 }
\DoxyCodeLine{229   \textcolor{keywordflow}{if} (verbose) \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random: ",a)'}) \textcolor{stringliteral}{'-\/-\/ PRNG tests -\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/'}}
\DoxyCodeLine{230   \textcolor{comment}{! Generate a random number, r1}}
\DoxyCodeLine{231   \textcolor{keyword}{call }random\_0d\_constructor(test\_rng, time, 1)}
\DoxyCodeLine{232   r1 = random\_01(test\_rng)}
\DoxyCodeLine{233   random\_unit\_tests = random\_unit\_tests .or. \&}
\DoxyCodeLine{234        test\_fn(verbose, abs(r1-\/4.75310122e-\/2)<1.e-\/9, \textcolor{stringliteral}{'first call'}, r1)}
\DoxyCodeLine{235 }
\DoxyCodeLine{236   \textcolor{comment}{! Check that we get a different number, r2, on a second call}}
\DoxyCodeLine{237   r2 = random\_01(test\_rng)}
\DoxyCodeLine{238   random\_unit\_tests = random\_unit\_tests .or. \&}
\DoxyCodeLine{239        test\_fn(verbose, abs(r2-\/2.71289742e-\/1)<1.e-\/9, \textcolor{stringliteral}{'consecutive test'}, r2)}
\DoxyCodeLine{240 }
\DoxyCodeLine{241   \textcolor{comment}{! Check that we can reproduce r1 by resetting the seed}}
\DoxyCodeLine{242   \textcolor{keyword}{call }random\_0d\_constructor(test\_rng, time, 1)}
\DoxyCodeLine{243   r2 = random\_01(test\_rng)}
\DoxyCodeLine{244   random\_unit\_tests = random\_unit\_tests .or. \&}
\DoxyCodeLine{245        test\_fn(verbose, abs(r2-\/r1)==0., \textcolor{stringliteral}{'reproduce test'}, r2)}
\DoxyCodeLine{246 }
\DoxyCodeLine{247   \textcolor{comment}{! Check that we get a different number, r2, with a different seed but same date}}
\DoxyCodeLine{248   \textcolor{keyword}{call }random\_0d\_constructor(test\_rng, time, 2)}
\DoxyCodeLine{249   r2 = random\_01(test\_rng)}
\DoxyCodeLine{250   random\_unit\_tests = random\_unit\_tests .or. \&}
\DoxyCodeLine{251        test\_fn(verbose, abs(r2-\/7.15508473e-\/1)<1.e-\/9, \textcolor{stringliteral}{'different seed test'}, r2)}
\DoxyCodeLine{252 }
\DoxyCodeLine{253   \textcolor{comment}{! Check that we get a different number, r2, for a different date but same seed}}
\DoxyCodeLine{254   time = set\_date(1903, 11, 21, 13, 0, 29)}
\DoxyCodeLine{255   \textcolor{keyword}{call }random\_0d\_constructor(test\_rng, time, 1)}
\DoxyCodeLine{256   r2 = random\_01(test\_rng)}
\DoxyCodeLine{257   random\_unit\_tests = random\_unit\_tests .or. \&}
\DoxyCodeLine{258        test\_fn(verbose, abs(r2-\/9.56667163e-\/1)<1.e-\/9, \textcolor{stringliteral}{'different date test'}, r2)}
\DoxyCodeLine{259 }
\DoxyCodeLine{260   \textcolor{keywordflow}{if} (verbose) \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random: ",a)'}) \textcolor{stringliteral}{'-\/-\/ index-\/based seeds -\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/'}}
\DoxyCodeLine{261   \textcolor{comment}{! Check index-\/based seed}}
\DoxyCodeLine{262   i = seed\_from\_index(hi,1,1)}
\DoxyCodeLine{263   random\_unit\_tests = random\_unit\_tests .or. test\_fn(verbose, i==1, \textcolor{stringliteral}{'seed from index (1,1)'}, ivalue=i)}
\DoxyCodeLine{264   j = seed\_from\_index(hi,ni+1,1)}
\DoxyCodeLine{265   random\_unit\_tests = random\_unit\_tests .or. test\_fn(verbose, j==i, \textcolor{stringliteral}{'seed from index (n+1,1)'}, ivalue=j)}
\DoxyCodeLine{266   i = seed\_from\_index(hi,ni,1)}
\DoxyCodeLine{267   random\_unit\_tests = random\_unit\_tests .or. test\_fn(verbose, i==6, \textcolor{stringliteral}{'seed from index (n,1)'}, ivalue=i)}
\DoxyCodeLine{268   j = seed\_from\_index(hi,0,1)}
\DoxyCodeLine{269   random\_unit\_tests = random\_unit\_tests .or. test\_fn(verbose, j==i, \textcolor{stringliteral}{'seed from index (0,1)'}, ivalue=j)}
\DoxyCodeLine{270   i = seed\_from\_index(hi,1,nj)}
\DoxyCodeLine{271   random\_unit\_tests = random\_unit\_tests .or. test\_fn(verbose, i==49, \textcolor{stringliteral}{'seed from index (1,n)'}, ivalue=i)}
\DoxyCodeLine{272   j = seed\_from\_index(hi,ni,nj+1)}
\DoxyCodeLine{273   random\_unit\_tests = random\_unit\_tests .or. test\_fn(verbose, j==i, \textcolor{stringliteral}{'seed from index (n,n+1)'}, ivalue=j)}
\DoxyCodeLine{274   i = seed\_from\_index(hi,ni,nj)}
\DoxyCodeLine{275   random\_unit\_tests = random\_unit\_tests .or. test\_fn(verbose, i==54, \textcolor{stringliteral}{'seed from index (n,n)'}, ivalue=i)}
\DoxyCodeLine{276   j = seed\_from\_index(hi,1,nj+1)}
\DoxyCodeLine{277   random\_unit\_tests = random\_unit\_tests .or. test\_fn(verbose, j==i, \textcolor{stringliteral}{'seed from index (1,n+1)'}, ivalue=j)}
\DoxyCodeLine{278 }
\DoxyCodeLine{279   \textcolor{keywordflow}{if} (.not.random\_unit\_tests) \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,a)'}) \textcolor{stringliteral}{'Passed unit tests'}}
\DoxyCodeLine{280   \textcolor{comment}{! The rest of these are not unit tests but statistical tests and as such}}
\DoxyCodeLine{281   \textcolor{comment}{! could fail for different sample sizes but happen to pass here.}}
\DoxyCodeLine{282 }
\DoxyCodeLine{283   \textcolor{comment}{! Check statistics of large samples for uniform generator}}
\DoxyCodeLine{284   mean = 0. ; var = 0. ; ar1 = 0. ; r2 = 0.}
\DoxyCodeLine{285   \textcolor{keywordflow}{do} i = 1, n\_samples}
\DoxyCodeLine{286     r1 = random\_01(test\_rng) -\/ 0.5}
\DoxyCodeLine{287     mean = mean + r1}
\DoxyCodeLine{288     var = var + r1**2}
\DoxyCodeLine{289     ar1 = ar1 + r1*r2}
\DoxyCodeLine{290     r2 = r1 \textcolor{comment}{! Keep copy of last value}}
\DoxyCodeLine{291 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{292   mean = mean / real(n\_samples) \textcolor{comment}{! Expected mean is 0}}
\DoxyCodeLine{293   var = var / real(n\_samples) \textcolor{comment}{! Expected variance is 1/12}}
\DoxyCodeLine{294   ar1 = ar1 / real(n\_samples-\/1) \textcolor{comment}{! Autocovariance}}
\DoxyCodeLine{295   std = sqrt(var) \textcolor{comment}{! Expected std is sqrt(1/12)}}
\DoxyCodeLine{296   r2 = mean*sqrt(real(12*n\_samples)) \textcolor{comment}{! Normalized error in mean}}
\DoxyCodeLine{297   r3 = std*sqrt(12.) \textcolor{comment}{! Normalized standard deviation}}
\DoxyCodeLine{298   r1 = ( ar1 * sqrt(real(n\_samples-\/1)) ) / var}
\DoxyCodeLine{299   \textcolor{keywordflow}{if} (verbose) \textcolor{keywordflow}{then}}
\DoxyCodeLine{300     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random: ",a)'}) \textcolor{stringliteral}{'-\/-\/ Uniform -\/0.5 .. 0.5 generator -\/-\/-\/-\/-\/-\/-\/-\/'}}
\DoxyCodeLine{301     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random: ",a,f12.9)'}) \textcolor{stringliteral}{'mean ='},mean,\textcolor{stringliteral}{'std ='},std,\textcolor{stringliteral}{'AR1 ='},ar1}
\DoxyCodeLine{302     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random: ",a,f12.9)'}) \textcolor{stringliteral}{'norm. mean ='},r2, \&}
\DoxyCodeLine{303                                              \textcolor{stringliteral}{'norm. std ='},r3,\textcolor{stringliteral}{'norm. AR1 ='},r1}
\DoxyCodeLine{304 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{305   random\_unit\_tests = random\_unit\_tests .or. \&}
\DoxyCodeLine{306                       test\_fn(verbose, abs(r2)<2., \&}
\DoxyCodeLine{307                               \textcolor{stringliteral}{'n>>1, mean within 2 sigma [uniform]'}, r2)}
\DoxyCodeLine{308   random\_unit\_tests = random\_unit\_tests .or. \&}
\DoxyCodeLine{309                       test\_fn(verbose, abs(r3-\/1.)<1./sqrt(real(n\_samples)), \&}
\DoxyCodeLine{310                               \textcolor{stringliteral}{'n>>1, std ~ 1/sqrt(12) [uniform]'}, r3-\/1.)}
\DoxyCodeLine{311   random\_unit\_tests = random\_unit\_tests .or. \&}
\DoxyCodeLine{312                       test\_fn(verbose, abs(r1)<2., \&}
\DoxyCodeLine{313                               \textcolor{stringliteral}{'n>>1, AR1 < std/sqrt(n) [uniform]'}, r1)}
\DoxyCodeLine{314 }
\DoxyCodeLine{315   \textcolor{comment}{! Check statistics of large samples for normal generator}}
\DoxyCodeLine{316   mean = 0. ; var = 0. ; ar1 = 0. ; r2 = 0.}
\DoxyCodeLine{317   \textcolor{keywordflow}{do} i = 1, n\_samples}
\DoxyCodeLine{318     r1 = random\_norm(test\_rng)}
\DoxyCodeLine{319     mean = mean + r1}
\DoxyCodeLine{320     var = var + r1**2}
\DoxyCodeLine{321     ar1 = ar1 + r1*r2}
\DoxyCodeLine{322     r2 = r1 \textcolor{comment}{! Keep copy of last value for AR calculation}}
\DoxyCodeLine{323 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{324   mean = mean / real(n\_samples)}
\DoxyCodeLine{325   var = var / real(n\_samples)}
\DoxyCodeLine{326   ar1 = ar1 / real(n\_samples)}
\DoxyCodeLine{327   std = sqrt(var)}
\DoxyCodeLine{328   r3 = 1./sqrt(real(n\_samples)) \textcolor{comment}{! Standard error of mean}}
\DoxyCodeLine{329   r2 = mean*sqrt(real(n\_samples)) \textcolor{comment}{! Normalized error in mean}}
\DoxyCodeLine{330   r3 = std \textcolor{comment}{! Normalized standard deviation}}
\DoxyCodeLine{331   r1 = ( ar1 * sqrt(real(n\_samples-\/1)) ) / var}
\DoxyCodeLine{332   \textcolor{keywordflow}{if} (verbose) \textcolor{keywordflow}{then}}
\DoxyCodeLine{333     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random: ",a)'}) \textcolor{stringliteral}{'-\/-\/ Normal distribution generator -\/-\/-\/-\/-\/-\/-\/-\/'}}
\DoxyCodeLine{334     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random: ",a,f12.9)'}) \textcolor{stringliteral}{'mean ='},mean,\textcolor{stringliteral}{'std ='},std,\textcolor{stringliteral}{'AR1 ='},ar1}
\DoxyCodeLine{335     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random: ",a,f12.9)'}) \textcolor{stringliteral}{'norm. error in mean ='},r2, \&}
\DoxyCodeLine{336                                              \textcolor{stringliteral}{'norm. standard deviation ='},r3,\textcolor{stringliteral}{'norm. AR1 ='},r1}
\DoxyCodeLine{337 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{338   random\_unit\_tests = random\_unit\_tests .or. \&}
\DoxyCodeLine{339                       test\_fn(verbose, abs(r2)<2., \&}
\DoxyCodeLine{340                               \textcolor{stringliteral}{'n>>1, mean within 2 sigma [norm]'}, r2)}
\DoxyCodeLine{341   random\_unit\_tests = random\_unit\_tests .or. \&}
\DoxyCodeLine{342                       test\_fn(verbose, abs(r3-\/1.)<1./sqrt(real(n\_samples)), \&}
\DoxyCodeLine{343                               \textcolor{stringliteral}{'n>>1, std ~ 1 [norm]'}, r3-\/1.)}
\DoxyCodeLine{344   random\_unit\_tests = random\_unit\_tests .or. \&}
\DoxyCodeLine{345                       test\_fn(verbose, abs(r1)<2., \&}
\DoxyCodeLine{346                               \textcolor{stringliteral}{'n>>1, AR1 < std/sqrt(n) [norm]'}, r1)}
\DoxyCodeLine{347 }
\DoxyCodeLine{348   \textcolor{keywordflow}{if} (verbose) \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random: ",a)'}) \textcolor{stringliteral}{'-\/-\/ 2d PRNG -\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/'}}
\DoxyCodeLine{349   \textcolor{comment}{! Check 2d random number generator 0..1}}
\DoxyCodeLine{350   \textcolor{keyword}{allocate}( r2d(hi\%isd:hi\%ied,hi\%jsd:hi\%jed) )}
\DoxyCodeLine{351   \textcolor{keyword}{call }random\_2d\_constructor(test\_rng, hi, time, 123)}
\DoxyCodeLine{352   r2d(:,:) = -\/999. \textcolor{comment}{! Use -\/9. to detect unset values}}
\DoxyCodeLine{353   \textcolor{keyword}{call }random\_2d\_01(test\_rng, hi, r2d)}
\DoxyCodeLine{354   \textcolor{keywordflow}{if} (any(abs(r2d(:,:)+999.)<=0.)) random\_unit\_tests=.true.}
\DoxyCodeLine{355   r1 = minval(r2d)}
\DoxyCodeLine{356   r2 = maxval(r2d)}
\DoxyCodeLine{357   random\_unit\_tests = random\_unit\_tests .or. test\_fn(verbose, r1>=0., \textcolor{stringliteral}{'2d all set'}, r1)}
\DoxyCodeLine{358   random\_unit\_tests = random\_unit\_tests .or. test\_fn(verbose, r2<=1., \textcolor{stringliteral}{'2d all valid'}, r2)}
\DoxyCodeLine{359   mean = sum( r2d(1:ni,1:nj) -\/ 0.5 )/real(ni*nj)}
\DoxyCodeLine{360   var = sum( (r2d(1:ni,1:nj) -\/ 0.5 -\/ mean)**2 )/real(ni*nj)}
\DoxyCodeLine{361   std = sqrt(var)}
\DoxyCodeLine{362   r3 = 1./sqrt(real(12*ni*nj)) \textcolor{comment}{! Standard error of mean}}
\DoxyCodeLine{363   r2 = mean*sqrt(real(12*ni*nj)) \textcolor{comment}{! Normalized error in mean}}
\DoxyCodeLine{364   r3 = std*sqrt(12.) \textcolor{comment}{! Normalized standard deviation}}
\DoxyCodeLine{365   \textcolor{keywordflow}{if} (verbose) \textcolor{keywordflow}{then}}
\DoxyCodeLine{366     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random: ",a)'}) \textcolor{stringliteral}{'2D uniform 0..1 generator'}}
\DoxyCodeLine{367     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random: ",a,f12.9)'}) \textcolor{stringliteral}{'mean ='},mean,\textcolor{stringliteral}{'std ='},std}
\DoxyCodeLine{368     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random: ",a,f12.9)'}) \textcolor{stringliteral}{'norm. error in mean ='},r2}
\DoxyCodeLine{369     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random: ",a,f12.9)'}) \textcolor{stringliteral}{'norm. standard deviation ='},r3}
\DoxyCodeLine{370 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{371   random\_unit\_tests = random\_unit\_tests .or. \&}
\DoxyCodeLine{372                       test\_fn(verbose, abs(r2)<2., \&}
\DoxyCodeLine{373                               \textcolor{stringliteral}{'2d, mean within 2 sigma [uniform]'}, r2)}
\DoxyCodeLine{374   random\_unit\_tests = random\_unit\_tests .or. \&}
\DoxyCodeLine{375                       test\_fn(verbose, abs(r3-\/1.)<1./sqrt(real(ni*nj)), \&}
\DoxyCodeLine{376                               \textcolor{stringliteral}{'2d, std ~ 1/sqrt(12) [uniform]'}, r3-\/1.)}
\DoxyCodeLine{377   \textcolor{keywordflow}{if} (verbose) \textcolor{keywordflow}{then}}
\DoxyCodeLine{378     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random:")'})}
\DoxyCodeLine{379     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random:",8f8.5)'}) r2d}
\DoxyCodeLine{380     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random:")'})}
\DoxyCodeLine{381 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{382 }
\DoxyCodeLine{383   \textcolor{comment}{! Check 2d normal random number generator}}
\DoxyCodeLine{384   \textcolor{keyword}{call }random\_2d\_norm(test\_rng, hi, r2d)}
\DoxyCodeLine{385   mean = sum( r2d(1:ni,1:nj) )/real(ni*nj)}
\DoxyCodeLine{386   var = sum( r2d(1:ni,1:nj)**2 )/real(ni*nj)}
\DoxyCodeLine{387   std = sqrt(var)}
\DoxyCodeLine{388   r3 = 1./sqrt(real(ni*nj)) \textcolor{comment}{! Standard error of mean}}
\DoxyCodeLine{389   r2 = mean*sqrt(real(ni*nj)) \textcolor{comment}{! Normalized error in mean}}
\DoxyCodeLine{390   r3 = std \textcolor{comment}{! Normalized standard deviation}}
\DoxyCodeLine{391   \textcolor{keywordflow}{if} (verbose) \textcolor{keywordflow}{then}}
\DoxyCodeLine{392     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random: ",a)'}) \textcolor{stringliteral}{'2D normal generator'}}
\DoxyCodeLine{393     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random: ",a,f12.9)'}) \textcolor{stringliteral}{'mean ='},mean,\textcolor{stringliteral}{'std ='},std}
\DoxyCodeLine{394     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random: ",a,f12.9)'}) \textcolor{stringliteral}{'norm. error in mean ='},r2}
\DoxyCodeLine{395     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,"random: ",a,f12.9)'}) \textcolor{stringliteral}{'norm. standard deviation ='},r3}
\DoxyCodeLine{396 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{397   random\_unit\_tests = random\_unit\_tests .or. \&}
\DoxyCodeLine{398                       test\_fn(verbose, abs(r2)<2., \&}
\DoxyCodeLine{399                               \textcolor{stringliteral}{'2d, mean within 2 sigma [norm]'}, r2)}
\DoxyCodeLine{400   random\_unit\_tests = random\_unit\_tests .or. \&}
\DoxyCodeLine{401                       test\_fn(verbose, abs(r3-\/1.)<1./sqrt(real(ni*nj)), \&}
\DoxyCodeLine{402                               \textcolor{stringliteral}{'2d, std ~ 1/sqrt(12) [norm]'}, r3-\/1.)}
\DoxyCodeLine{403 }
\DoxyCodeLine{404   \textcolor{comment}{! Clean up}}
\DoxyCodeLine{405   \textcolor{keyword}{deallocate}(r2d)}
\DoxyCodeLine{406   \textcolor{keyword}{deallocate}(hi)}
\DoxyCodeLine{407 }
\DoxyCodeLine{408   \textcolor{keywordflow}{if} (.not.random\_unit\_tests) \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(1x,a)'}) \textcolor{stringliteral}{'Passed statistical tests'}}
\DoxyCodeLine{409 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__random_a8605064631f07f3a7e9704422732c81c}\label{namespacemom__random_a8605064631f07f3a7e9704422732c81c}} 
\index{mom\_random@{mom\_random}!seed\_from\_index@{seed\_from\_index}}
\index{seed\_from\_index@{seed\_from\_index}!mom\_random@{mom\_random}}
\doxysubsubsection{\texorpdfstring{seed\_from\_index()}{seed\_from\_index()}}
{\footnotesize\ttfamily integer function mom\+\_\+random\+::seed\+\_\+from\+\_\+index (\begin{DoxyParamCaption}\item[{type(hor\+\_\+index\+\_\+type), intent(in)}]{HI,  }\item[{integer, intent(in)}]{i,  }\item[{integer, intent(in)}]{j }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Create seed from position index. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em hi} & Horizontal index structure \\
\hline
\mbox{\texttt{ in}}  & {\em i} & i-\/index (of h-\/cell) \\
\hline
\mbox{\texttt{ in}}  & {\em j} & j-\/index (of h-\/cell) \\
\hline
\end{DoxyParams}


Definition at line 155 of file M\+O\+M\+\_\+random.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{155   \textcolor{keywordtype}{type}(hor\_index\_type), \textcolor{keywordtype}{intent(in)} :: HI\textcolor{comment}{ !< Horizontal index structure}}
\DoxyCodeLine{156   \textcolor{keywordtype}{integer},              \textcolor{keywordtype}{intent(in)} :: i\textcolor{comment}{ !< i-\/index (of h-\/cell)}}
\DoxyCodeLine{157   \textcolor{keywordtype}{integer},              \textcolor{keywordtype}{intent(in)} :: j\textcolor{comment}{ !< j-\/index (of h-\/cell)}}
\DoxyCodeLine{158   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{159   \textcolor{keywordtype}{integer} :: ig, jg, ni, nj, ij}
\DoxyCodeLine{160 }
\DoxyCodeLine{161   ni = hi\%niglobal}
\DoxyCodeLine{162   nj = hi\%njglobal}
\DoxyCodeLine{163   \textcolor{comment}{! Periodicity is assumed here but does not break non-\/periodic models}}
\DoxyCodeLine{164   ig = mod(hi\%idg\_offset + i -\/ 1 + ni, ni)+1}
\DoxyCodeLine{165   jg = max(hi\%jdg\_offset + j, 0)}
\DoxyCodeLine{166   \textcolor{keywordflow}{if} (jg>nj) \textcolor{keywordflow}{then} \textcolor{comment}{! Tri-\/polar hard-\/coded until we put needed info in HI **TODO**}}
\DoxyCodeLine{167     jg = 2*nj+1-\/jg}
\DoxyCodeLine{168     ig = ni+1-\/ig}
\DoxyCodeLine{169 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{170   seed\_from\_index = ig + ni*(jg-\/1)}
\DoxyCodeLine{171 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__random_a48fc0d0eab640dedd565d14603e5b85c}\label{namespacemom__random_a48fc0d0eab640dedd565d14603e5b85c}} 
\index{mom\_random@{mom\_random}!seed\_from\_time@{seed\_from\_time}}
\index{seed\_from\_time@{seed\_from\_time}!mom\_random@{mom\_random}}
\doxysubsubsection{\texorpdfstring{seed\_from\_time()}{seed\_from\_time()}}
{\footnotesize\ttfamily integer function mom\+\_\+random\+::seed\+\_\+from\+\_\+time (\begin{DoxyParamCaption}\item[{type(time\+\_\+type), intent(in)}]{Time }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Return a seed derived as hash of values in Time. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em time} & Current model time \\
\hline
\end{DoxyParams}


Definition at line 139 of file M\+O\+M\+\_\+random.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{139   \textcolor{keywordtype}{type}(time\_type), \textcolor{keywordtype}{intent(in)}    :: Time\textcolor{comment}{ !< Current model time}}
\DoxyCodeLine{140   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{141   \textcolor{keywordtype}{integer} :: yr,mo,dy,hr,mn,sc,s1,s2}
\DoxyCodeLine{142 }
\DoxyCodeLine{143   \textcolor{keyword}{call }get\_date(time,yr,mo,dy,hr,mn,sc)}
\DoxyCodeLine{144   s1 = sc + 61*(mn + 61*hr) + 379 \textcolor{comment}{! Range 379 .. 89620}}
\DoxyCodeLine{145   \textcolor{comment}{! Fun fact: 2147483647 is the eighth Mersenne prime.}}
\DoxyCodeLine{146   \textcolor{comment}{! This is not the reason for using 2147483647 here. It is the}}
\DoxyCodeLine{147   \textcolor{comment}{! largest integer of kind=4.}}
\DoxyCodeLine{148   s2 = modulo(dy + 32*(mo + 13*yr), 2147483647\_4) \textcolor{comment}{! Range 0 .. 2147483646}}
\DoxyCodeLine{149   seed\_from\_time = ieor(s1*4111, s2)}
\DoxyCodeLine{150 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__random_a32dd7680a649486b9b96efc7c21ee7a1}\label{namespacemom__random_a32dd7680a649486b9b96efc7c21ee7a1}} 
\index{mom\_random@{mom\_random}!test\_fn@{test\_fn}}
\index{test\_fn@{test\_fn}!mom\_random@{mom\_random}}
\doxysubsubsection{\texorpdfstring{test\_fn()}{test\_fn()}}
{\footnotesize\ttfamily logical function mom\+\_\+random\+::test\+\_\+fn (\begin{DoxyParamCaption}\item[{logical, intent(in)}]{verbose,  }\item[{logical, intent(in)}]{good,  }\item[{character(len=$\ast$), intent(in)}]{label,  }\item[{real, intent(in), optional}]{rvalue,  }\item[{integer, intent(in), optional}]{ivalue }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Convenience function for reporting result of test. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em verbose} & Verbosity \\
\hline
\mbox{\texttt{ in}}  & {\em good} & True if pass, false otherwise \\
\hline
\mbox{\texttt{ in}}  & {\em label} & Label for messages \\
\hline
\mbox{\texttt{ in}}  & {\em rvalue} & Result of calculation \\
\hline
\mbox{\texttt{ in}}  & {\em ivalue} & Result of calculation \\
\hline
\end{DoxyParams}


Definition at line 414 of file M\+O\+M\+\_\+random.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{414   \textcolor{keywordtype}{logical},          \textcolor{keywordtype}{intent(in)} :: verbose\textcolor{comment}{ !< Verbosity}}
\DoxyCodeLine{415   \textcolor{keywordtype}{logical},          \textcolor{keywordtype}{intent(in)} :: good\textcolor{comment}{ !< True if pass, false otherwise}}
\DoxyCodeLine{416   \textcolor{keywordtype}{character(len=*)}, \textcolor{keywordtype}{intent(in)} :: label\textcolor{comment}{ !< Label for messages}}
\DoxyCodeLine{417 \textcolor{keywordtype}{  real},             \textcolor{keywordtype}{intent(in)} :: rvalue\textcolor{comment}{ !< Result of calculation}}
\DoxyCodeLine{418   \textcolor{keywordtype}{integer},          \textcolor{keywordtype}{intent(in)} :: ivalue\textcolor{comment}{ !< Result of calculation}}
\DoxyCodeLine{419   \textcolor{keywordtype}{optional} :: rvalue, ivalue}
\DoxyCodeLine{420 }
\DoxyCodeLine{421   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(ivalue)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{422     \textcolor{keywordflow}{if} (.not. good) \textcolor{keywordflow}{then}}
\DoxyCodeLine{423       \textcolor{keyword}{write}(stdout,\textcolor{stringliteral}{'(1x,a,i10,1x,a,a)'}) \textcolor{stringliteral}{'random: result ='},ivalue,label,\textcolor{stringliteral}{' <-\/-\/-\/-\/-\/-\/-\/ FAIL!'}}
\DoxyCodeLine{424       \textcolor{keyword}{write}(stderr,\textcolor{stringliteral}{'(1x,a,i10,1x,a,a)'}) \textcolor{stringliteral}{'random: result ='},ivalue,label,\textcolor{stringliteral}{' <-\/-\/-\/-\/-\/-\/-\/ FAIL!'}}
\DoxyCodeLine{425     \textcolor{keywordflow}{elseif} (verbose) \textcolor{keywordflow}{then}}
\DoxyCodeLine{426       \textcolor{keyword}{write}(stdout,\textcolor{stringliteral}{'(1x,a,i10,1x,a)'}) \textcolor{stringliteral}{'random: result ='},ivalue,label}
\DoxyCodeLine{427 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{428   \textcolor{keywordflow}{else}}
\DoxyCodeLine{429     \textcolor{keywordflow}{if} (.not. good) \textcolor{keywordflow}{then}}
\DoxyCodeLine{430       \textcolor{keyword}{write}(stdout,\textcolor{stringliteral}{'(1x,a,1pe15.8,1x,a,a)'}) \textcolor{stringliteral}{'random: result ='},rvalue,label,\textcolor{stringliteral}{' <-\/-\/-\/-\/-\/-\/-\/ FAIL!'}}
\DoxyCodeLine{431       \textcolor{keyword}{write}(stderr,\textcolor{stringliteral}{'(1x,a,1pe15.8,1x,a,a)'}) \textcolor{stringliteral}{'random: result ='},rvalue,label,\textcolor{stringliteral}{' <-\/-\/-\/-\/-\/-\/-\/ FAIL!'}}
\DoxyCodeLine{432     \textcolor{keywordflow}{elseif} (verbose) \textcolor{keywordflow}{then}}
\DoxyCodeLine{433       \textcolor{keyword}{write}(stdout,\textcolor{stringliteral}{'(1x,a,1pe15.8,1x,a)'}) \textcolor{stringliteral}{'random: result ='},rvalue,label}
\DoxyCodeLine{434 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{435 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{436   test\_fn = .not. good}
\DoxyCodeLine{437 }

\end{DoxyCode}
