A Modular Arbitrary-Order Ocean-Atmosphere Model -- Stochastic implementation
stat.f90
Go to the documentation of this file.
1 
2 ! stat.f90
3 !
4 !> Statistics accumulators
5 !
6 !> @copyright
7 !> 2015 Lesley De Cruz & Jonathan Demaeyer.
8 !> See LICENSE.txt for license information.
9 !
10 !---------------------------------------------------------------------------!
11 
12 
13 
14 MODULE stat
15  USE params, only: ndim
16  IMPLICIT NONE
17 
18  PRIVATE
19 
20  INTEGER :: i=0 !< Number of stats accumulated
21 
22  ! Vectors holding the stats
23  REAL(KIND=8), DIMENSION(:), ALLOCATABLE :: m !< Vector storing the inline mean
24  REAL(KIND=8), DIMENSION(:), ALLOCATABLE :: mprev !< Previous mean vector
25  REAL(KIND=8), DIMENSION(:), ALLOCATABLE :: v !< Vector storing the inline variance
26  REAL(KIND=8), DIMENSION(:), ALLOCATABLE :: mtmp
27 
28 
29  PUBLIC :: acc,init_stat,mean,var,iter,reset
30 
31  CONTAINS
32 
33  !> Initialise the accumulators
34  SUBROUTINE init_stat
35  INTEGER :: AllocStat
36 
37  ALLOCATE(m(0:ndim),mprev(0:ndim),v(0:ndim),mtmp(0:ndim), stat=allocstat)
38  IF (allocstat /= 0) stop '*** Not enough memory ***'
39  m=0.d0
40  mprev=0.d0
41  v=0.d0
42  mtmp=0.d0
43 
44  END SUBROUTINE init_stat
45 
46  !> Accumulate one state
47  SUBROUTINE acc(x)
48  IMPLICIT NONE
49  REAL(KIND=8), DIMENSION(0:ndim), INTENT(IN) :: x
50  i=i+1
51  mprev=m+(x-m)/i
52  mtmp=mprev
53  mprev=m
54  m=mtmp
55  v=v+(x-mprev)*(x-m)
56  END SUBROUTINE acc
57 
58  !> Function returning the mean
59  FUNCTION mean()
60  REAL(KIND=8), DIMENSION(0:ndim) :: mean
61  mean=m
62  END FUNCTION mean
63 
64  !> Function returning the variance
65  FUNCTION var()
66  REAL(KIND=8), DIMENSION(0:ndim) :: var
67  var=v/(i-1)
68  END FUNCTION var
69 
70  !> Function returning the number of data accumulated
71  FUNCTION iter()
72  INTEGER :: iter
73  iter=i
74  END FUNCTION iter
75 
76  !> Routine resetting the accumulators
77  SUBROUTINE reset
78  m=0.d0
79  mprev=0.d0
80  v=0.d0
81  i=0
82  END SUBROUTINE reset
83 
84 
85  END MODULE stat
integer ndim
Number of variables (dimension of the model)
Definition: params.f90:85
Statistics accumulators.
Definition: stat.f90:14
integer i
Number of stats accumulated.
Definition: stat.f90:20
subroutine, public acc(x)
Accumulate one state.
Definition: stat.f90:48
real(kind=8) function, dimension(0:ndim), public var()
Function returning the variance.
Definition: stat.f90:66
real(kind=8), dimension(:), allocatable v
Vector storing the inline variance.
Definition: stat.f90:25
subroutine, public init_stat
Initialise the accumulators.
Definition: stat.f90:35
The model parameters module.
Definition: params.f90:18
real(kind=8), dimension(:), allocatable m
Vector storing the inline mean.
Definition: stat.f90:23
real(kind=8) function, dimension(0:ndim), public mean()
Function returning the mean.
Definition: stat.f90:60
real(kind=8), dimension(:), allocatable mprev
Previous mean vector.
Definition: stat.f90:24
real(kind=8), dimension(:), allocatable mtmp
Definition: stat.f90:26
integer function, public iter()
Function returning the number of data accumulated.
Definition: stat.f90:72
subroutine, public reset
Routine resetting the accumulators.
Definition: stat.f90:78