A Modular Arbitrary-Order Ocean-Atmosphere Model -- Stochastic implementation
stoch_mod.f90
Go to the documentation of this file.
1 
2 ! stoch_mod.f90
3 !
4 !> Utility module containing the stochastic related routines.
5 !
6 !> @copyright
7 !> 2018 Jonathan Demaeyer.
8 !> See LICENSE.txt for license information.
9 !
10 !---------------------------------------------------------------------------!
11 !
12 !> @remark
13 !
14 !---------------------------------------------------------------------------!
15 MODULE stoch_mod
16 
17  USE params, only: ndim,natm
18  USE sf_def, only: sf
19  IMPLICIT NONE
20 
21  PRIVATE
22 
23  INTEGER :: iset=0
24  REAL(KIND=8) :: gset
25 
28 
29 CONTAINS
30 
31  FUNCTION gasdev()
32  REAL(KIND=8) :: gasdev
33  REAL(KIND=8) :: fac,rsq,v1,v2,r
34  IF (iset.eq.0) THEN
35  DO
36  CALL random_number(r)
37  v1=2.d0*r-1.
38  CALL random_number(r)
39  v2=2.d0*r-1.
40  rsq=v1**2+v2**2
41  IF (rsq.lt.1.d0.and.rsq.ne.0.d0) EXIT
42  ENDDO
43  fac=sqrt(-2.*log(rsq)/rsq)
44  gset=v1*fac
45  gasdev=v2*fac
46  iset=1
47  ELSE
48  gasdev=gset
49  iset=0
50  ENDIF
51  RETURN
52  END FUNCTION gasdev
53 
54  !> Routine to fill a vector with standard Gaussian noise process values
55  !> @param dW Vector to fill
56  SUBROUTINE stoch_vec(dW)
57  REAL(KIND=8), DIMENSION(0:ndim), INTENT(INOUT) :: dW
58  INTEGER :: i
59  DO i=1,ndim
60  dw(i)=gasdev()
61  ENDDO
62  END SUBROUTINE stoch_vec
63 
64  !> routine to fill the atmospheric component of a vector with standard gaussian noise process values
65  !> @param dW vector to fill
66  subroutine stoch_atm_vec(dW)
67  real(kind=8), dimension(0:ndim), intent(inout) :: dW
68  integer :: i
69  do i=1,2*natm
70  dw(i)=gasdev()
71  enddo
72  end subroutine stoch_atm_vec
73 
74  !> routine to fill the resolved atmospheric component of a vector with standard gaussian noise process values
75  !> @param dW vector to fill
76  subroutine stoch_atm_res_vec(dW)
77  real(kind=8), dimension(0:ndim), intent(inout) :: dW
78  integer :: i
79  dw=0.d0
80  do i=1,2*natm
81  IF (sf(i)==0) dw(i)=gasdev()
82  enddo
83  end subroutine stoch_atm_res_vec
84 
85  !> routine to fill the unresolved atmospheric component of a vector with standard gaussian noise process values
86  !> @param dW vector to fill
87  subroutine stoch_atm_unres_vec(dW)
88  real(kind=8), dimension(0:ndim), intent(inout) :: dW
89  integer :: i
90  dw=0.d0
91  do i=1,2*natm
92  IF (sf(i)==1) dw(i)=gasdev()
93  enddo
94  end subroutine stoch_atm_unres_vec
95 
96  !> routine to fill the oceanic component of a vector with standard gaussian noise process values
97  !> @param dW vector to fill
98  subroutine stoch_oc_vec(dW)
99  real(kind=8), dimension(0:ndim), intent(inout) :: dW
100  integer :: i
101  do i=2*natm+1,ndim
102  dw(i)=gasdev()
103  enddo
104  end subroutine stoch_oc_vec
105 
106  !> routine to fill the resolved oceanic component of a vector with standard gaussian noise process values
107  !> @param dW vector to fill
108  subroutine stoch_oc_res_vec(dW)
109  real(kind=8), dimension(0:ndim), intent(inout) :: dW
110  integer :: i
111  dw=0.d0
112  do i=2*natm+1,ndim
113  IF (sf(i)==0) dw(i)=gasdev()
114  enddo
115  end subroutine stoch_oc_res_vec
116 
117  !> routine to fill the unresolved oceanic component of a vector with standard gaussian noise process values
118  !> @param dW vector to fill
119  subroutine stoch_oc_unres_vec(dW)
120  real(kind=8), dimension(0:ndim), intent(inout) :: dW
121  integer :: i
122  dw=0.d0
123  do i=2*natm+1,ndim
124  IF (sf(i)==1) dw(i)=gasdev()
125  enddo
126  end subroutine stoch_oc_unres_vec
127 
128 END MODULE stoch_mod
subroutine, public stoch_oc_unres_vec(dW)
routine to fill the unresolved oceanic component of a vector with standard gaussian noise process val...
Definition: stoch_mod.f90:120
integer ndim
Number of variables (dimension of the model)
Definition: params.f90:85
subroutine, public stoch_atm_unres_vec(dW)
routine to fill the unresolved atmospheric component of a vector with standard gaussian noise process...
Definition: stoch_mod.f90:88
real(kind=8) function, public gasdev()
Definition: stoch_mod.f90:32
subroutine, public stoch_atm_res_vec(dW)
routine to fill the resolved atmospheric component of a vector with standard gaussian noise process v...
Definition: stoch_mod.f90:77
subroutine, public stoch_oc_res_vec(dW)
routine to fill the resolved oceanic component of a vector with standard gaussian noise process value...
Definition: stoch_mod.f90:109
integer iset
Definition: stoch_mod.f90:23
integer, dimension(:), allocatable, public sf
Unresolved variable definition vector.
Definition: sf_def.f90:23
integer natm
Number of atmospheric basis functions.
Definition: params.f90:83
subroutine, public stoch_vec(dW)
Routine to fill a vector with standard Gaussian noise process values.
Definition: stoch_mod.f90:57
subroutine, public stoch_oc_vec(dW)
routine to fill the oceanic component of a vector with standard gaussian noise process values ...
Definition: stoch_mod.f90:99
The model parameters module.
Definition: params.f90:18
Utility module containing the stochastic related routines.
Definition: stoch_mod.f90:15
real(kind=8) gset
Definition: stoch_mod.f90:24
subroutine, public stoch_atm_vec(dW)
routine to fill the atmospheric component of a vector with standard gaussian noise process values ...
Definition: stoch_mod.f90:67
Module to select the resolved-unresolved components.
Definition: sf_def.f90:12