-
Notifications
You must be signed in to change notification settings - Fork 103
/
SHCilmToVector.f95
220 lines (184 loc) · 6.11 KB
/
SHCilmToVector.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
subroutine SHCilmToVector(cilm, vector, lmax, exitstatus)
!------------------------------------------------------------------------------
!
! This subroutine will convert an array of spherical harmonic
! coefficients Cilm to an ordered 1D vector.
!
! Calling Parameters
!
! IN
! cilm Input spherical harmonic coefficients with dimension
! cilm(2, lmax+1, lmax+1).
! lmax Maximum spherical harmonic degree of the input coefficients:
!
! OUT
! vector 1D vector of ordered spherical harmonic coefficients with
! dimension (lmax+1)**2. The ordering is described in
! YilmIndexVector.
!
! 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 ftypes
implicit none
real(dp), intent(in) :: cilm(:,:,:)
real(dp), intent(out) :: vector(:)
integer(int32), intent(in) :: lmax
integer(int32), intent(out), optional :: exitstatus
integer(int32) :: i, l, m
if (present(exitstatus)) exitstatus = 0
if (size(cilm(:,1,1)) < 2 .or. size(cilm(1,:,1)) < lmax+1 .or. &
size(cilm(1,1,:)) < lmax+1) then
print*, "Error --- SHCilmToVector"
print*, "CILM must be dimensioned as (2, LMAX+1, LMAX + 1)."
print*, "LMAX = ", lmax
print*, "Dimension of CILM = ", size(cilm(:,1,1)), size(cilm(1,:,1)), &
size(cilm(1,1,:))
if (present(exitstatus)) then
exitstatus = 1
return
else
stop
end if
end if
if (size(vector(:)) < (lmax+1)**2) then
print*, "Error --- SHCilmToVector"
print*, "VECTOR must be have dimension (LMAX+1)**2."
print*, "LMAX = ", lmax
print*, "Dimension of VECTOR = ", size(vector(:))
if (present(exitstatus)) then
exitstatus = 1
return
else
stop
end if
end if
if (lmax < 0) then
print*, "Error --- SHCilmToVector"
print*, "LMAX must be positive."
print*, "LMAX = ", lmax
if (present(exitstatus)) then
exitstatus = 2
return
else
stop
end if
end if
i = 0
do l = 0, lmax
do m = 0, l
i = i + 1
vector(i) = cilm(1, l+1, m+1)
end do
do m = 1, l, 1
i = i + 1
vector(i) = cilm(2, l+1, m+1)
end do
end do
end subroutine SHCilmToVector
subroutine SHVectorToCilm(vector, cilm, lmax, exitstatus)
!------------------------------------------------------------------------------
!
! This subroutine will convert a 1D ordered vector of spherical harmonic
! coefficients to a 3D array Cilm(i, l, m).
!
! Calling Parameters
!
! IN
! vector 1D vector of ordered spherical harmonic coefficients with
! dimension (lmax+1)**2. The ordering is described in
! YilmIndexVector.
! lmax Maximum spherical harmonic degree of the input coefficients.
!
! OUT
! cilm Output spherical harmonic coefficients with dimension
! cilm(2, lmax+1, lmax+1).
!
! 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) 2016, SHTOOLS
! All rights reserved.
!
!------------------------------------------------------------------------------
use ftypes
implicit none
real(dp), intent(out) :: cilm(:,:,:)
real(dp), intent(in) :: vector(:)
integer(int32), intent(in) :: lmax
integer(int32), intent(out), optional :: exitstatus
integer(int32) :: k, i, l, m
if (present(exitstatus)) exitstatus = 0
if (size(cilm(:,1,1)) < 2 .or. size(cilm(1,:,1)) < lmax+1 &
.or. size(cilm(1,1,:)) < lmax+1) then
print*, "Error --- SHVectorToCilm"
print*, "CILM must be dimensioned as (2, LMAX+1, LMAX + 1)."
print*, "LMAX = ", lmax
print*, "Dimension of CILM = ", size(cilm(:,1,1)), size(cilm(1,:,1)), &
size(cilm(1,1,:))
if (present(exitstatus)) then
exitstatus = 1
return
else
stop
end if
end if
if (size(vector(:)) < (lmax+1)**2) then
print*, "Error --- SHVectorToCilm"
print*, "VECTOR must be have dimension (LMAX+1)**2."
print*, "LMAX = ", lmax
print*, "Dimension of VECTOR = ", size(vector(:))
if (present(exitstatus)) then
exitstatus = 1
return
else
stop
end if
end if
if (lmax < 0) then
print*, "Error --- SHVectorToCilm"
print*, "LMAX must be positive."
print*, "LMAX = ", lmax
if (present(exitstatus)) then
exitstatus = 2
return
else
stop
end if
end if
l = 0
m = 0
i = 1
cilm(1,1,1) = vector(1)
i = 2
do k=2, (lmax+1)**2, 1
m = m + 1
if (m > l .and. i == 1) then
i = 2
m = 1
else if (m > l .and. i == 2) then
l = l + 1
m = 0
i = 1
end if
cilm(i,l+1,m+1) = vector(k)
end do
end subroutine SHVectorToCilm