-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalfq_module.f90
318 lines (259 loc) · 8.25 KB
/
alfq_module.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
module alfq_module
! calculates normalised associated Legendre functions
! using three point recurrence
use kind_module, only: i4b, qp
implicit none
private
! Source: Based on Fukushima (2011)
! Author: T. Enomoto
! Usage:
! Calculates the values of normalized associated Legendre polynomials
! at latitudes lat
! NB:
! normalised to 1 by default. factor (-1)**m is not included.
real(kind=qp), public, dimension(:,:), allocatable :: alfq_anm, alfq_bnm
real(kind=qp), public, dimension(:), allocatable :: alfq_cm, alfq_dm
real(kind=qp), private, parameter :: quad_min = 2.0_qp**(-16494)
integer(kind=i4b), private :: alfq_ntrunc = 0
real(kind=qp), private :: pstart
integer, private :: retain = 0
public :: alfq_init, alfq_clean, alfq_calc, alfq_calc_m, &
alfq_calcps, alfq_calcpn, alfq_checksum, alfq_test, alfq_test_checksum
contains
subroutine alfq_init(ntrunc)
integer(kind=i4b), intent(in) :: ntrunc
integer(kind=i4b) :: n, m
! print *, "alfq_init"
if (alfq_ntrunc==ntrunc) then
retain = retain + 1
! print *, retain
return
end if
alfq_ntrunc = ntrunc
! print *, "Allocating alfq_anm, alfq_bnm, alfq_cm, alfq_dm"
allocate(alfq_cm(0:ntrunc),alfq_dm(ntrunc))
do m=0, ntrunc-1
alfq_cm(m) = sqrt(2.0_qp*m+3.0_qp)
end do
do m=1, ntrunc
alfq_dm(m) = sqrt(1.0_qp + 0.5_qp/real(m,kind=qp))
end do
allocate(alfq_anm(ntrunc,0:ntrunc),alfq_bnm(ntrunc,0:ntrunc))
do m=0, ntrunc
do n=m+2, ntrunc
alfq_anm(n,m) = sqrt((2.0_qp*n+1.0_qp)/real(n**2-m**2,kind=qp))
alfq_bnm(n,m) = alfq_anm(n,m)*sqrt((n+m-1.0_qp)*(n-m-1.0_qp)/(2.0_qp*n-3.0_qp))
alfq_anm(n,m) = alfq_anm(n,m)*sqrt(2.0_qp*n-1.0_qp)
end do
end do
retain = retain + 1
! print *, retain
end subroutine alfq_init
subroutine alfq_clean()
! print *, "alfq_clean"
retain = retain - 1
! print *, retain
if (retain<1) then
! print *, "Deallocating alfq_anm, alfq_bnm, alfq_cm, alfq_dm"
deallocate(alfq_anm,alfq_bnm,alfq_cm,alfq_dm)
alfq_ntrunc = 0
end if
end subroutine alfq_clean
subroutine alfq_calc(lat,alf,p00)
real(kind=qp), dimension(:), intent(in) :: lat
real(kind=qp), dimension(0:,0:,:), intent(out) :: alf
real(kind=qp), intent(in), optional :: p00
integer(kind=i4b) :: j, m, n, jmax, jmaxh, mmax
real(kind=qp), dimension(0:size(alf,1)-1) :: pmm, pnm
real(kind=qp), dimension(size(lat)) :: sinlat, coslat
mmax = size(alf,1) - 1
if (present(p00)) then
pstart = p00
else
pstart = sqrt(0.5_qp)
end if
jmax = size(lat)
if (jmax<1) then
return
end if
alf(:,:,:) = 0.0_qp
sinlat(:) = sin(lat(:))
coslat(:) = cos(lat(:))
pmm(0) = pstart
do j=1, min(jmax,size(alf,3))
call alfq_calcps(coslat(j),alfq_dm,pmm)
do m=0, mmax-1
pnm(m) = pmm(m)
alf(m,m,j) = pmm(m)
call alfq_calcpn(sinlat(j),m,alfq_anm(:,m),alfq_bnm(:,m),alfq_cm(m),pnm)
alf(m+1:mmax,m,j) = pnm(m+1:mmax)
end do
alf(mmax,mmax,j) = pmm(mmax)
end do
end subroutine alfq_calc
subroutine alfq_calc_m(m,lat,alfm,p00)
integer(kind=i4b), intent(in) :: m
real(kind=qp), dimension(:), intent(in) :: lat
real(kind=qp), dimension(0:,:), intent(out) :: alfm
real(kind=qp), intent(in), optional :: p00
integer(kind=i4b) :: j, n, jmax, jmaxh, mmax
real(kind=qp), dimension(0:size(alfm,1)-1) :: pmm, pnm
real(kind=qp), dimension(size(lat)) :: sinlat, coslat
mmax = size(alfm,1) - 1
if (present(p00)) then
pstart = p00
else
pstart = sqrt(0.5_qp)
end if
jmax = size(lat)
if (jmax<1) then
return
end if
alfm(:,:) = 0.0_qp
sinlat(:) = sin(lat(:))
coslat(:) = cos(lat(:))
pmm(0) = pstart
do j=1, min(jmax,size(alfm,2))
call alfq_calcps(coslat(j),alfq_dm,pmm)
if (m/=mmax) then
pnm(m) = pmm(m)
alfm(m,j) = pmm(m)
call alfq_calcpn(sinlat(j),m,alfq_anm(:,m),alfq_bnm(:,m),alfq_cm(m),pnm)
alfm(m+1:mmax,j) = pnm(m+1:mmax)
else
alfm(mmax,j) = pmm(mmax)
end if
end do
end subroutine alfq_calc_m
subroutine alfq_calcps(u,d,ps)
real(kind=qp), intent(in) :: u ! coslat
real(kind=qp), dimension(:), intent(in) :: d
real(kind=qp), dimension(0:), intent(inout) :: ps
integer(kind=i4b) :: m, nmax
nmax = size(ps)-1
do m=1, nmax
ps(m) = (d(m)*u)*ps(m-1)
if (abs(ps(m))<quad_min) then
exit
end if
end do
ps(m:) = 0.0_qp
end subroutine alfq_calcps
subroutine alfq_calcpn(t,m,an,bn,c,pn)
real(kind=qp), intent(in) :: t ! sinlat
integer(kind=i4b), intent(in) :: m
real(kind=qp), dimension(:), intent(in) :: an, bn
real(kind=qp), intent(in) :: c
real(kind=qp), dimension(0:), intent(inout) :: pn
integer(kind=i4b) :: n, nmax
nmax = size(pn)-1
if (m+1>nmax) then
return
endif
pn(m+1) = c*t*pn(m)
do n=m+2, nmax
pn(n) = an(n)*t*pn(n-1)-bn(n)*pn(n-2)
end do
end subroutine alfq_calcpn
function alfq_checksum(wgt,pj) result(s)
real(kind=qp), dimension(:), intent(in) :: wgt
real(kind=qp), dimension(:), intent(in) :: pj
real(kind=qp) :: y, c, s, t
integer(kind=i4b) :: j, jmaxh
jmaxh = size(pj)
c = 0.0_qp
s = 0.0_qp
do j = 1, jmaxh
y = 2.0_qp * wgt(j) * pj(j) * pj(j) - c
t = s + y
c = (t - s) - y
s = t
end do
end function alfq_checksum
subroutine alfq_test(ntrunc,nlat,un)
use math_module, only: rad2deg=>math_rad2degq
use glatwgtq_module, only: glatwgtq_calc
integer(kind=i4b), intent(in) :: ntrunc, nlat
integer(kind=i4b), intent(in), optional :: un
real(kind=qp), dimension(:), allocatable :: lat, wgt
real(kind=qp), dimension(:,:,:), allocatable :: alf
real(kind=qp) :: t1, t2
integer(kind=i4b) :: j
print *, "# ----- alfq_test() -----"
print *, "ntrunc=", ntrunc, " nlat=", nlat
allocate(lat(nlat),wgt(nlat))
allocate(alf(0:ntrunc,0:ntrunc,nlat/2))
call glatwgtq_calc(lat,wgt)
call alfq_init(ntrunc)
call cpu_time(t1)
call alfq_calc(lat(1:nlat/2),alf)
call cpu_time(t2)
print *, "alfq_calc cpu time=", t2-t1
if (present(un)) then
write(unit=un,rec=1) alf
end if
deallocate(alf)
deallocate(lat,wgt)
call alfq_clean()
end subroutine alfq_test
subroutine alfq_test_checksum(ntrunc,nlat,un)
use kind_module, only: qp, i4b
use glatwgtq_module, only: glatwgtq_calc
implicit none
integer(kind=i4b), intent(in) :: ntrunc, nlat
integer(kind=i4b), intent(in), optional :: un
real(kind=qp) :: xx, dd, x, dx, p00
integer(kind=i4b) :: jmaxh, m, n, j, mm, nn
real(kind=qp), dimension(:), allocatable :: &
lat, sinlat, coslat, wgt, pmm, pnm
real(kind=qp), dimension(:,:), allocatable :: pjm, pjn
print *, "# ----- alfq_test_checksum() -----"
print *, "x=\int pnm pnm dx error"
print *, "ntrunc=", ntrunc, " nlat=", nlat
jmaxh = nlat/2
allocate(lat(nlat),sinlat(jmaxh),coslat(jmaxh),wgt(nlat))
call glatwgtq_calc(lat,wgt)
sinlat(:) = sin(lat(1:jmaxh))
coslat(:) = cos(lat(1:jmaxh))
allocate(pmm(0:ntrunc),pnm(0:ntrunc), &
pjm(jmaxh,0:ntrunc),pjn(jmaxh,0:ntrunc))
xx = 1.0_qp
dd = 0.0_qp
dx = 0.0_qp
nn = 0
mm = 0
p00 = sqrt(0.5_qp)
call alfq_init(ntrunc)
do j=1, jmaxh
pmm(0) = p00
call alfq_calcps(coslat(j),alfq_dm,pmm)
pjm(j,:) = pmm(:)
end do
do m=0, ntrunc
do j=1, jmaxh
pnm(m) = pjm(j,m)
pjn(j,m) = pjm(j,m)
if (m<ntrunc) then
call alfq_calcpn(sinlat(j),m,alfq_anm(:,m),alfq_bnm(:,m),alfq_cm(m),pnm)
pjn(j,m+1:ntrunc) = pnm(m+1:ntrunc)
end if
end do
do n=m, ntrunc
x = alfq_checksum(wgt,pjn(:,n))
dx = 1.0_qp - x
if (present(un)) then
write(unit=un,fmt=*) n, m, x, abs(dx)!, dd
end if
if (abs(dx)>dd) then
xx = x
dd = abs(dx)
mm = m
nn = n
end if
end do
end do
print *, "x=", xx, " with max error= ", dd, " at (n,m)=(", nn, ",", mm, ")"
deallocate(lat,sinlat,coslat,wgt,pmm,pnm,pjm,pjn)
call alfq_clean()
end subroutine alfq_test_checksum
end module alfq_module