-
Notifications
You must be signed in to change notification settings - Fork 103
/
ComputeDMap.f95
388 lines (321 loc) · 12.4 KB
/
ComputeDMap.f95
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
subroutine ComputeDMap(Dij, dh_mask, n_dh, lmax, sampling, degrees, exitstatus)
!------------------------------------------------------------------------------
!
! This subroutine will compute the matrix D(lm,l'm') for the spatiospectral
! concentration problem using the input grid DH_MASK that defines the
! concentration region. The input grid must be sampled according to the
! Driscoll and Healy sampling theorem and must possess values of 1 inside
! the concentration region, and 0 elsewhere. The grid possess N_DH samples
! in latitude, and either N_DH samples in longitude for SAMPLING=1
! or 2*N_DH samples in longitude for SAMPLING=2. Elements of the matrix D
! (which are ordered accoring to YilmIndex) are computed up to a maximum
! degree LMAX. If the optional vector DEGREES is specified, then the matrix
! will be computed only for degrees l where DEGREES(l+1) is not zero.
!
! It should be noted that the elements of D are computed approximately.
! These are explicitly calculated using
!
! Dlm,l'm' = 1/(4 pi) Int_Omega F Yl'm' dOmega
!
! where
!
! F = Ylm * DH_MASK.
!
! This integral would be computed exactly if F had a bandwith of
! L = N_DH/2 - 1. However, since DH_MASK has an infinite bandwidth, this
! will not be true. However, if L is larger than LMAX, then we might expect
! that the spherical harmonic expansions will be good approximations. This
! needs to be verified by chosing different values of N_DH and comparing
! the results.
!
! Calling Parameters
!
! IN
! dh_mask Integer grid sampled according to the Driscoll and
! Healy sampling theorem. A value of 1 indicates that
! the grid node is in the concentration domain, and a
! value of 0 indicates that it is outside. Dimensioned as
! (n_dh, n_dh) for SAMPLING = 1 or (n_dh, 2*n_dh) for
! SAMPLING = 2.
! n_dh The number of latitude samples in the Driscoll and
! Healy sampled grid.
! lmax Maximum spherical harmonic degree of elements in the
! matrix D.
!
! IN, OPTIONAL
! SAMPLING 1 (default) corresponds to equal sampling (n_dh, n_dh),
! whereas 2 corresponds to equal spaced grids
! (n_dh, 2*n_dh).
! degrees Specify those degrees of the coupling matrix to
! compute. If degrees(l+1) is zero, degree l will not
! be computed.
! OUT
! Dij Elements of the matrix D, which is symmetric, and
! whose elements are packed into a 1D array according
! to YilmINDEX. Dimesions of Dij are
! ((lmax+1)**2, (lmax+1)**2).
!
! OPTIONAL (OUT)
! exitstatus If present, instead of executing a STOP when an error
! is encountered, the variable exitstatus will be
! returned describing the error.
! 0 = No errors;
! 1 = Improper dimensions of input array;
! 2 = Improper bounds for input variable;
! 3 = Error allocating memory;
! 4 = File IO error.
!
! Copyright (c) 2005-2019, SHTOOLS
! All rights reserved.
!
!------------------------------------------------------------------------------
use SHTOOLS, only : SHExpandDH, PlmIndex, PlmBar, SHCilmToVector, &
YilmIndexVector
use ftypes
implicit none
real(dp), intent(out) :: Dij(:,:)
integer(int32), intent(in) :: dh_mask(:,:), n_dh, lmax
integer(int32), intent(in), optional:: sampling, degrees(:)
integer(int32), intent(out), optional :: exitstatus
integer(int32) :: nlat, nlong, lmax_dh, astat, i, j, k, l, m, max_mask, min_mask
real(dp), allocatable :: f(:,:), plm(:), clm(:,:,:), vec(:)
real(dp) :: colat, lat_int, temp(2*n_dh), lon, lon_int
if (present(exitstatus)) exitstatus = 0
if (size(Dij(:,1)) < (lmax+1)**2 .or. size(Dij(1,:)) < (lmax+1)**2) then
print*, "Error --- ComputeDMap"
print*, "Dij must be dimesioned as ((LMAX+1)**2, (LMAX+1)**2)."
print*, "Dij is dimensioned as ", size(Dij(:,1)), size(Dij(1,:))
if (present(exitstatus)) then
exitstatus = 1
return
else
stop
end if
end if
if (mod(n_dh,2) /= 0) then
print*, "Error --- ComputeDMap"
print*, "Number of samples in latitude must be even for the " // &
"Driscoll and Healy sampling theorem."
print*, "N_DH = ", n_dh
if (present(exitstatus)) then
exitstatus = 2
return
else
stop
end if
end if
nlat = n_dh
lat_int = acos(-1.0_dp) / dble(nlat)
if (present(sampling)) then
if (sampling == 1) then
nlong = nlat
lon_int = 2.0_dp * lat_int
else if (sampling == 2) then
nlong = 2 * nlat
lon_int = lat_int
else
print*, "Error --- ComputeDMap"
print*, "SAMPLING must be either 1 (equally sampled) " // &
"or 2 (equally spaced)."
print*, "SAMPLING = ", sampling
if (present(exitstatus)) then
exitstatus = 2
return
else
stop
end if
end if
else
nlong = nlat
lon_int = 2.0_dp * lat_int
end if
if (size(dh_mask(:,1)) < nlat .or. size(dh_mask(1,:)) < nlong) then
print*, "Error --- ComputeDMap"
print*, "DH_MASK must be dimensioned as ", nlat, nlong
print*, "Dimensions of DH_MASK are ", size(dh_mask(:,1)), &
size(dh_mask(1,:))
if (present(exitstatus)) then
exitstatus = 1
return
else
stop
end if
end if
lmax_dh = n_dh / 2 - 1
if (lmax_dh < lmax) then
print*, "Error --- ComputeDMap"
print*, "The effective bandwith of the input grid DH_MASK must be " //&
"greater or equal than LMAX."
print*, "LMAX = ", lmax
print*, "Effective bandwidth of DH_MASK = ", lmax_dh
if (present(exitstatus)) then
exitstatus = 2
return
else
stop
end if
end if
if (present(degrees)) then
if (size(degrees) < lmax+1) then
print*, "Error --- ComputeDm"
print*, "DEGREES must have dimension LMAX+1, where LMAX is ", lmax
print*, "Input array is dimensioned as ", size(degrees)
if (present(exitstatus)) then
exitstatus = 1
return
else
stop
end if
end if
end if
allocate (f(nlat,nlong), stat = astat)
if (astat /= 0) then
print*, "Error --- ComputeDMap"
print*, "Problem allocating memory for grid F(NLAT, NLONG)."
print*, "NLAT = ", nlat
print*, "NLONG = ", nlong
if (present(exitstatus)) then
exitstatus = 3
return
else
stop
end if
end if
allocate (plm((lmax+1)*(lmax+2)/2), stat = astat)
if (astat /= 0) then
print*, "Error --- ComputeDMap"
print*, "Problem allocating memory for grid PLM((LMAX+1)*(LMAX+2)/2)."
print*, "LMAX = ", lmax
if (present(exitstatus)) then
exitstatus = 3
return
else
stop
end if
end if
allocate (clm(2, lmax+1, lmax+1), stat = astat)
if (astat /= 0) then
print*, "Error --- ComputeDMap"
print*, "Problem allocating memory for CLM(2, LMAX+1, LMAX+1)."
if (present(exitstatus)) then
exitstatus = 3
return
else
stop
end if
end if
allocate (vec((lmax+1)**2), stat = astat)
if (astat /= 0) then
print*, "Error --- ComputeDMap"
print*, "Problem allocating memory for VEC((LMAX+1)**2)."
print*, "LMAX = ", lmax
if (present(exitstatus)) then
exitstatus = 3
return
else
stop
end if
end if
min_mask = minval(dh_mask(1:nlat, 1:nlong))
max_mask = maxval(dh_mask(1:nlat, 1:nlong))
if (min_mask < 0 .or. max_mask > 1) then
print*, "Error --- ComputeDMap"
print*, "DH_MASK must consist of 0s exterior to the " // &
"concentration region"
print*, "and 1s inside of the concentration region."
print*, "Minimum value of DH_MASK = ", min_mask
print*, "Maximum value of DH_MASK = ", max_mask
if (present(exitstatus)) then
exitstatus = 2
return
else
stop
end if
end if
!--------------------------------------------------------------------------
!
! Compute elements of Dij. Start at high degrees, and work towards
! low degrees. Dij is symmetric.
!
!--------------------------------------------------------------------------
dij = 0.0_dp
do l = lmax, 0, -1
if (present(degrees)) then
if (degrees(l+1) == 0) cycle
end if
do m = 0, l
do j = 1, 2
if (m == 0 .and. j == 2) cycle
!--------------------------------------------------------------
!
! Make Map of Yilm * mask
!
!--------------------------------------------------------------
do k = 1, nlong
lon = dble(k-1) * lon_int
if (j == 1) then
temp(k) = cos(dble(m) * lon)
else
temp(k) = sin(dble(m) * lon)
end if
end do
do k = 1, nlat
colat = dble(k-1) * lat_int
if (present(exitstatus)) then
call plmbar(plm, l, cos(colat), exitstatus=exitstatus)
if (exitstatus /= 0) return
else
call plmbar(plm, l, cos(colat))
end if
f(k, 1:nlong) = plm(plmindex(l,m)) * temp(1:nlong) &
* dble(dh_mask(k, 1:nlong))
end do
i = YilmIndexVector(j, l, m)
if (present(exitstatus)) then
if (present(sampling)) then
call SHExpandDH(f, n_dh, clm, lmax_dh, &
sampling = sampling, lmax_calc = l, &
exitstatus = exitstatus)
if (exitstatus /= 0) return
else
call SHExpandDH(f, n_dh, clm, lmax_dh, sampling = 1, &
lmax_calc = l, exitstatus = exitstatus)
if (exitstatus /= 0) return
end if
call SHCilmToVector(clm, vec, l, exitstatus = exitstatus)
if (exitstatus /= 0) return
else
if (present(sampling)) then
call SHExpandDH(f, n_dh, clm, lmax_dh, &
sampling = sampling, lmax_calc = l)
else
call SHExpandDH(f, n_dh, clm, lmax_dh, sampling = 1, &
lmax_calc = l)
end if
call SHCilmToVector(clm, vec, l)
end if
dij(i, 1:(l+1)**2) = vec(1:(l+1)**2)
if (l /= 0) dij(1:(l+1)**2 - 1, i) = vec(1:(l+1)**2 - 1)
end do
end do
end do
if (present(degrees)) then
do l=0, lmax
do m=0, l
if (degrees(l+1) == 0) then
dij(:, YilmIndexVector(1, l, m)) = 0.0_dp
dij(YilmIndexVector(1, l, m), :) = 0.0_dp
if (m > 0) then
dij(:, YilmIndexVector(2, l, m)) = 0.0_dp
dij(YilmIndexVector(2, l, m), :) = 0.0_dp
end if
end if
end do
end do
end if
call plmbar(plm, -1, -1.0_dp)
deallocate (f)
deallocate (plm)
deallocate (clm)
deallocate (vec)
end subroutine ComputeDMap