-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathplmbar_d1.f90
277 lines (240 loc) · 8.58 KB
/
plmbar_d1.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
!
!
!
SUBROUTINE PLMBAR_D1(P, DP, LMAX, Z, CSPHASE)
!
! --- Implemented into SELEN by GS on August 7, 2008, with very slight
! modifications with respect to the original by M. Wieczoreck. I
! use real*4 precision, here, as for PLMBAR.
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! This function evalutates all of the normalized associated Legendre
! functions up to degree lmax. The functions are initially scaled by
! 10^280 sin^m in order to minimize the effects of underflow at large m
! near the poles (see Holmes and Featherstone 2002, J. Geodesy, 76, 279-299).
! On a Mac OSX system with a maximum allowable double precision value of
! 2.225073858507203E-308 the scaled portion of the algorithm will not overflow
! for degrees less than or equal to 2800.
!
! For each value of m, the rescaling factor is computed as rescalem=rescalem*sin(theta),
! with the intial value of rescalem being equal to 1/scalef (which is here equal
! to 10^280). This will gradually reduce this huge number to a tiny number, and will
! ultimately underflow. In order to prevent this underflow, when rescalem becomes less than
! 10^(-280), the subsequent rescaling factors of sin(theta) will be directly applied to Plm, and then this
! number will be multipled by the old value of rescalem.
!
! Temporary variables in saved in an allocated array. In order to explicitly deallocate this
! memory, call this routine with a spherical harmonic degree of -1.
!
! Calling Parameters:
! OUT
! p: A vector of all associated Legendgre polynomials evaluated at
! z up to lmax. The lenght must by greater or equal to (lmax+1)*(lmax+2)/2.
! dp: A vector of all first derivatives of the normalized Legendgre polynomials evaluated at
! z up to lmax with dimension (lmax+1).
! IN
! lmax: Maximum spherical harmonic degree to compute.
! z: cos(colatitude) or sin(latitude).
! OPTIONAL (IN)
! csphase: 1: Do not include the phase factor of (-1)^m
! -1: Apply the phase factor of (-1)^m.
!
! Notes:
!
! 1. The employed normalization is the "geophysical convention." The integral of
! (plm*cos(m theta))**2 or (plm*sin (m theta))**2 over all space is 4 pi.
! 2. The integral of plm**2 over (-1,1) is 2 * (2 - delta(0,m))
! 3. The index of the array p corresponds to l*(l+1)/2 + m + 1. As such
! the array p should be dimensions as (lmax+1)*(lmax+2)/2 in the
! calling routine.
! 4. Derivatives are calculated using the unnormalized identities
! P'l,m = ( (l+m) Pl-1,m - l z Plm ) / (1-z**2) (for l>m), and
! P'll = - l z Pll / (1-z**2) (for l=m).
! 5. The derivative is not defined at z=+-1 for all m>0, and is therefore not
! calculated here.
! 6. The default is to exlude the Condon-Shortley phase of (-1)^m.
!
!
! Dependencies: CSPHASE_DEFAULT
!
! Written by Mark Wieczorek September 25, 2005.
!
! Copyright (c) 2005, Mark A. Wieczorek
! All rights reserved.
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! use SHTOOLS, only: CSPHASE_DEFAULT
!
implicit none
integer, intent(in) :: lmax
real*8, intent(out) :: p(:), dp(:)
real*8, intent(in) :: z
integer, intent(in), optional :: csphase
real*8 :: pm2, pm1, pmm, plm, rescalem, phase, u, scalef
real*8, allocatable, save :: f1(:), f2(:), sqr(:)
integer :: k, kstart, m, l, sdim, astat(3)
integer, save :: lmax_old = 0
!
integer CSPHASE_DEFAULT
!
if (lmax == -1) then
if (allocated(sqr)) deallocate(sqr)
if (allocated(f1)) deallocate(f1)
if (allocated(f2)) deallocate(f2)
lmax_old = 0
return
endif
sdim = (lmax+1)*(lmax+2)/2
if (size(p) < sdim) then
print*, "Error --- PlmBar_d1"
print*, "P must be dimensioned as (LMAX+1)*(LMAX+2)/2 where LMAX is ", lmax
print*, "Input array is dimensioned ", size(p)
stop
elseif (size(dp) < sdim) then
print*, "Error --- PlmBar_d1"
print*, "DP must be dimensioned as (LMAX+1)*(LMAX+2)/2 where LMAX is ", lmax
print*, "Input array is dimensioned ", size(dp)
stop
elseif (lmax < 0) then
print*, "Error --- PlmBar_d1"
print*, "LMAX must be greater than or equal to 0."
print*, "Input value is ", lmax
stop
elseif(abs(z) > 1.) then
print*, "Error --- PlmBar_d1"
print*, "ABS(Z) must be less than or equal to 1."
print*, "Input value is ", z
stop
elseif (abs(z) == 1.) then
print*, "Error --- PlmBar_d1"
print*, "Derivative can not be calculated at Z = 1 or -1."
print*, "Input value is ", z
stop
endif
if (present(csphase)) then
if (csphase == -1) then
phase = -1.
elseif (csphase == 1) then
phase = 1.
else
print*, "PlmBar_d1 --- Error"
print*, "CSPHASE must be 1 (exclude) or -1 (include)."
stop
endif
else
phase = dble(CSPHASE_DEFAULT)
endif
scalef = 1E-20
if (lmax /= lmax_old) then
if (allocated(sqr)) deallocate(sqr)
if (allocated(f1)) deallocate(f1)
if (allocated(f2)) deallocate(f2)
allocate(sqr(2*lmax+1), stat=astat(1))
allocate(f1((lmax+1)*(lmax+2)/2), stat=astat(2))
allocate(f2((lmax+1)*(lmax+2)/2), stat=astat(3))
if (astat(1) /= 0 .or. astat(2) /= 0 .or. astat(3) /= 0) then
print*, "PlmBar_d1 --- Error"
print*, "Problem allocating arrays SQR, F1 and F2", astat(1), astat(2), astat(3)
stop
endif
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Precompute square roots of integers that are used several times.
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
do l=1, 2*lmax+1
sqr(l) = sqrt(float(l))
enddo
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Precompute multiplicative factors used in recursion relationships
! Plmbar(l,m) = x*f1(l,m)*Plmbar(l-1,m) - Plmbar(l-2,m)*f2(l,m)
! k = l*(l+1)/2 + m + 1
! Note that prefactors are not used for the case when m=l and m=l-1,
! as a different recursion is used for these two values.
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
k = 3
do l=2, lmax, 1
k = k + 1
f1(k) = sqr(2*l-1) * sqr(2*l+1) / float(l)
f2(k) = float(l-1) * sqr(2*l+1) / sqr(2*l-3) / float(l)
do m=1, l-2
k = k+1
f1(k) = sqr(2*l+1) * sqr(2*l-1) / sqr(l+m) / sqr(l-m)
f2(k) = sqr(2*l+1) * sqr(l-m-1) * sqr(l+m-1) &
/ sqr(2*l-3) / sqr(l+m) / sqr(l-m)
enddo
k = k + 2
enddo
lmax_old = lmax
endif
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Calculate P(l,0). These are not scaled.
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
u = sqrt((1.-z)*(1.+z)) ! sin(theta)
pm2 = 1.
p(1) = 1.
dp(1)= 0.
if (lmax == 0) return
pm1 = sqr(3)*z
p(2) = pm1
dp(2) = sqr(3)
k = 2
do l = 2, lmax, 1
k = k+l
plm = f1(k)*z*pm1-f2(k)*pm2
p(k) = plm
dp(k) = float(l) * ( sqr(2*l+1) / sqr(2*l-1) * &
pm1 - z * plm ) / u**2
pm2 = pm1
pm1 = plm
enddo
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Calculate P(m,m), P(m+1,m), and P(l,m)
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
pmm = sqr(2)*scalef
rescalem = 1.0/scalef
kstart = 1
do m = 1, lmax - 1, 1
rescalem = rescalem*u
! Calculate P(m,m)
kstart = kstart+m+1
pmm = phase * pmm * sqr(2*m+1) / sqr(2*m)
p(kstart) = pmm*rescalem
dp(kstart) = -float(m) * z * p(kstart) / u**2
pm2 = pmm
! Calculate P(m+1,m)
k = kstart+m+1
pm1 = z * sqr(2*m+3) * pmm
p(k) = pm1*rescalem
dp(k) = ( sqr(2*m+3) * p(k-m-1) - z * float(m+1) * p(k)) / u**2
! Calculate P(l,m)
do l = m+2, lmax, 1
k = k+l
plm = z*f1(k)*pm1-f2(k)*pm2
p(k) = plm*rescalem
dp(k) = ( sqr(2*l+1) * sqr(l-m) * sqr(l+m) / sqr(2*l-1) * &
p(k-l) - z * float(l) * p(k)) / u**2
pm2 = pm1
pm1 = plm
enddo
enddo
! Calculate P(lmax,lmax)
rescalem = rescalem*u
kstart = kstart+m+1
pmm = phase * pmm * sqr(2*lmax+1) / sqr(2*lmax)
p(kstart) = pmm*rescalem
dp(kstart) = -float(lmax) * z * p(kstart) / u**2
end subroutine PlmBar_d1
!
!
!
!
!