1 | module math_mod |
---|
2 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
3 | !!! |
---|
4 | !!! Purpose: The module contains all the mathematical SUBROUTINE used in the PEM |
---|
5 | !!! |
---|
6 | !!! Author: Adapted from Schorgofer MSIM (N.S, Icarus 2010), impletented here by LL |
---|
7 | !!! |
---|
8 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
9 | |
---|
10 | implicit none |
---|
11 | |
---|
12 | !======================================================================= |
---|
13 | contains |
---|
14 | !======================================================================= |
---|
15 | |
---|
16 | SUBROUTINE deriv1(z,nz,y,y0,ybot,dzY) |
---|
17 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
18 | !!! |
---|
19 | !!! Purpose: Compute the first derivative of a function y(z) on an irregular grid |
---|
20 | !!! |
---|
21 | !!! Author: From N.S (N.S, Icarus 2010), impletented here by LL |
---|
22 | !!! |
---|
23 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
24 | ! first derivative of a function y(z) on irregular grid |
---|
25 | ! upper boundary conditions: y(0) = y0 |
---|
26 | ! lower boundary condition.: yp = ybottom |
---|
27 | |
---|
28 | implicit none |
---|
29 | |
---|
30 | integer, intent(in) :: nz ! number of layer |
---|
31 | real, intent(in) :: z(nz) ! depth layer |
---|
32 | real, intent(in) :: y(nz) ! function which needs to be derived |
---|
33 | real, intent(in) :: y0,ybot ! boundary conditions |
---|
34 | real, intent(out) :: dzY(nz) ! derivative of y w.r.t depth |
---|
35 | ! local |
---|
36 | integer :: j |
---|
37 | real :: hm, hp, c1, c2, c3 |
---|
38 | |
---|
39 | hp = z(2) - z(1) |
---|
40 | c1 = z(1)/(hp*z(2)) |
---|
41 | c2 = 1/z(1) - 1/(z(2) - z(1)) |
---|
42 | c3 = -hp/(z(1)*z(2)) |
---|
43 | dzY(1) = c1*y(2) + c2*y(1) + c3*y0 |
---|
44 | do j = 2,nz - 1 |
---|
45 | hp = z(j + 1) - z(j) |
---|
46 | hm = z(j) - z(j - 1) |
---|
47 | c1 = +hm/(hp*(z(j + 1) - z(j - 1))) |
---|
48 | c2 = 1/hm - 1/hp |
---|
49 | c3 = -hp/(hm*(z(j + 1) - z(j - 1))) |
---|
50 | dzY(j) = c1*y(j + 1) + c2*y(j) + c3*y(j - 1) |
---|
51 | enddo |
---|
52 | dzY(nz) = (ybot - y(nz - 1))/(2.*(z(nz) - z(nz - 1))) |
---|
53 | |
---|
54 | END SUBROUTINE deriv1 |
---|
55 | |
---|
56 | !======================================================================= |
---|
57 | |
---|
58 | SUBROUTINE deriv2_simple(z,nz,y,y0,yNp1,yp2) |
---|
59 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
60 | !!! |
---|
61 | !!! Purpose: Compute the second derivative of a function y(z) on an irregular grid |
---|
62 | !!! |
---|
63 | !!! Author: N.S (raw copy/paste from MSIM) |
---|
64 | !!! |
---|
65 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
66 | ! second derivative y_zz on irregular grid |
---|
67 | ! boundary conditions: y(0) = y0, y(nz + 1) = yNp1 |
---|
68 | |
---|
69 | implicit none |
---|
70 | |
---|
71 | integer, intent(in) :: nz |
---|
72 | real, intent(in) :: z(nz), y(nz), y0, yNp1 |
---|
73 | real, intent(out) :: yp2(nz) |
---|
74 | integer :: j |
---|
75 | real :: hm, hp, c1, c2, c3 |
---|
76 | |
---|
77 | c1 = +2./((z(2) - z(1))*z(2)) |
---|
78 | c2 = -2./((z(2) - z(1))*z(1)) |
---|
79 | c3 = +2./(z(1)*z(2)) |
---|
80 | yp2(1) = c1*y(2) + c2*y(1) + c3*y0 |
---|
81 | do j = 2,nz - 1 |
---|
82 | hp = z(j + 1) - z(j) |
---|
83 | hm = z(j) - z(j - 1) |
---|
84 | c1 = +2./(hp*(z(j + 1) - z(j - 1))) |
---|
85 | c2 = -2./(hp*hm) |
---|
86 | c3 = +2./(hm*(z(j + 1) - z(j-1))) |
---|
87 | yp2(j) = c1*y(j + 1) + c2*y(j) + c3*y(j - 1) |
---|
88 | enddo |
---|
89 | yp2(nz) = (yNp1 - 2*y(nz) + y(nz - 1))/(z(nz) - z(nz - 1))**2 |
---|
90 | |
---|
91 | END SUBROUTINE deriv2_simple |
---|
92 | |
---|
93 | !======================================================================= |
---|
94 | |
---|
95 | SUBROUTINE deriv1_onesided(j,z,nz,y,dy_zj) |
---|
96 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
97 | !!! |
---|
98 | !!! Purpose: First derivative of function y(z) at z(j) one-sided derivative on irregular grid |
---|
99 | !!! |
---|
100 | !!! Author: N.S (raw copy/paste from MSIM) |
---|
101 | !!! |
---|
102 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
103 | |
---|
104 | implicit none |
---|
105 | |
---|
106 | integer, intent(in) :: nz, j |
---|
107 | real, intent(in) :: z(nz), y(nz) |
---|
108 | real, intent(out) :: dy_zj |
---|
109 | real :: h1, h2, c1, c2, c3 |
---|
110 | |
---|
111 | if (j < 1 .or. j > nz - 2) then |
---|
112 | dy_zj = -1. |
---|
113 | else |
---|
114 | h1 = z(j + 1) - z(j) |
---|
115 | h2 = z(j + 2)- z(j + 1) |
---|
116 | c1 = -(2*h1 + h2)/(h1*(h1 + h2)) |
---|
117 | c2 = (h1 + h2)/(h1*h2) |
---|
118 | c3 = -h1/(h2*(h1 + h2)) |
---|
119 | dy_zj = c1*y(j) + c2*y(j + 1) + c3*y(j + 2) |
---|
120 | endif |
---|
121 | |
---|
122 | END SUBROUTINE deriv1_onesided |
---|
123 | |
---|
124 | !======================================================================= |
---|
125 | |
---|
126 | PURE SUBROUTINE colint(y,z,nz,i1,i2,integral) |
---|
127 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
128 | !!! |
---|
129 | !!! Purpose: Column integrates y on irregular grid |
---|
130 | !!! |
---|
131 | !!! Author: N.S (raw copy/paste from MSIM) |
---|
132 | !!! |
---|
133 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
134 | |
---|
135 | implicit none |
---|
136 | |
---|
137 | integer, intent(in) :: nz, i1, i2 |
---|
138 | real, intent(in) :: y(nz), z(nz) |
---|
139 | real, intent(out) :: integral |
---|
140 | integer i |
---|
141 | real dz(nz) |
---|
142 | |
---|
143 | dz(1) = (z(2) - 0.)/2 |
---|
144 | do i = 2,nz - 1 |
---|
145 | dz(i) = (z(i + 1) - z(i - 1))/2. |
---|
146 | enddo |
---|
147 | dz(nz) = z(nz) - z(nz - 1) |
---|
148 | integral = sum(y(i1:i2)*dz(i1:i2)) |
---|
149 | |
---|
150 | END SUBROUTINE colint |
---|
151 | |
---|
152 | !======================================================================= |
---|
153 | |
---|
154 | SUBROUTINE findroot(y1,y2,z1,z2,zr) |
---|
155 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
156 | !!! |
---|
157 | !!! Purpose: Compute the root zr, between two values y1 and y2 at depth z1,z2 |
---|
158 | !!! |
---|
159 | !!! Author: LL |
---|
160 | !!! |
---|
161 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
162 | |
---|
163 | implicit none |
---|
164 | |
---|
165 | real, intent(in) :: y1, y2 ! difference between surface water density and at depth [kg/m^3] |
---|
166 | real, intent(in) :: z1, z2 ! depth [m] |
---|
167 | real, intent(out) :: zr ! depth at which we have zero |
---|
168 | |
---|
169 | zr = (y1*z2 - y2*z1)/(y1 - y2) |
---|
170 | |
---|
171 | END SUBROUTINE findroot |
---|
172 | |
---|
173 | end module math_mod |
---|