-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsolvlevpop.F90
329 lines (310 loc) · 10.5 KB
/
solvlevpop.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
319
320
321
322
323
324
325
326
327
328
329
SUBROUTINE solvlevpop(NLEV,TRANSITION,density,SOLUTION)!,coolant)
use definitions
use healpix_types
! use maincode_module, only : p,iteration,pdr,gridpoint,nrays
implicit none
integer(kind=i4b), intent(in) :: NLEV
! integer(kind=i4b), intent(in) :: coolant
real(kind=dp), intent(in) :: density
real(kind=dp), intent(in) :: transition(1:NLEV,1:NLEV)
real(kind=dp), intent(out) :: SOLUTION(1:NLEV)
integer(kind=i4b) :: i,j!,ii
real(kind=dp) :: out1
real(kind=dp) :: A(1:NLEV,1:NLEV)
logical::call_writes
!real(kind=dp) :: temp_a(1:nlev,1:nlev),temp_solution(1:nlev)
A=0.0D0
! Fill the matrix
DO I=1,NLEV
OUT1=0.0D0
DO J=1,NLEV
OUT1=OUT1+TRANSITION(I,J)
A(I,J)=TRANSITION(J,I)
ENDDO
A(I,I)=-OUT1
ENDDO
! Initialize the solution array before calling the solver routine
DO I=1,NLEV
SOLUTION(I)=0.0D0
A(NLEV,I)=1.0D-8 !non-zero starting parameter to avoid division by zero.
ENDDO
SOLUTION(NLEV)=DENSITY*1.0D-8
!CALL GAUSS_JORDAN(A,NLEV,NLEV,SOLUTION,coolant,call_writes)
CALL GAUSS_JORDAN(A,NLEV,NLEV,SOLUTION,call_writes)
! Replace negative level populations due to numerical noise around 0
DO I=1,NLEV
if (solution(i).lt.0.0D0) solution(i)=0.0D0!1.0D-99!then !stop 'found negative solution!'
! write(6,*) '';write(6,*) 'found negative solution in p=',p;write(6,*) 'coolant=',coolant;write(6,*)''
! call gauss_jordan_writes(temp_a,nlev,nlev,temp_solution,coolant,i)
! stop
! endif
ENDDO
return
end subroutine
!C-----------------------------------------------------------------------
!C Standard Gauss-Jordon linear equation solver from Numerical Recipes
!C A(N,N) is an input matrix stored in an array of dimensions NPxNP
!C B(N,M) is an input matrix containing the M right-hand side vectors
!C stored in an array of dimensions NPxMPP
!C
!C On output, A(N,N) is replaced by its matrix inverse and B(N,M)
!C is replaced by the corresponding set of solution vectors
!C
!C Note: set NMAX to the maximum possible dimension (NLEVEL)
!C-----------------------------------------------------------------------
! SUBROUTINE GAUSS_JORDAN(A,N,NP,B,M,MPP,coolant)
! SUBROUTINE GAUSS_JORDAN(A,N,NP,B,coolant,call_writes)
SUBROUTINE GAUSS_JORDAN(A,N,NP,B,call_writes)
use definitions
use healpix_types
use maincode_module, only : gastemperature,p,iteration
IMPLICIT NONE
! integer(kind=i4b), intent(in) :: coolant
INTEGER(kind=i4b):: I,J,K,L,LL,IROW,ICOL
INTEGER(kind=i4b), intent(in):: N,NP!,M,MPP
integer(kind=i4b), PARAMETER :: NMAX=100
INTEGER(kind=i4b):: IPIV(1:NMAX),INDXR(1:NMAX),INDXC(1:NMAX)
real(kind=dp), intent(inout) :: A(1:NP,1:NP)
real(kind=dp), intent(inout) :: B(1:NP)!,1:MPP)
real(kind=dp) :: BIG,DUM,PIVINV
logical,intent(out)::call_writes
ICOL=0
IROW=0
IPIV=0
DO I=1,N
BIG=0.0D0
DO J=1,N
IF(IPIV(J).NE.1) THEN
DO K=1,N
IF(IPIV(K).EQ.0) THEN
IF(ABS(A(J,K)).GE.BIG) THEN
BIG=ABS(A(J,K))
IROW=J
ICOL=K
ENDIF
ELSE IF(IPIV(K).GT.1) THEN
PRINT *,'ERROR! Singular matrix in GAUSS_JORDAN'
call_writes=.true.
return
! write(6,*) 'Crashed in first loop'
! write(6,*) 'grid point = ',p, ' coolant = ',coolant
! write(6,*) 'gastemperature = ',gastemperature(p)
! STOP
ENDIF
ENDDO
ENDIF
ENDDO
IPIV(ICOL)=IPIV(ICOL)+1
IF(IROW.NE.ICOL) THEN
DO L=1,N
DUM=A(IROW,L)
A(IROW,L)=A(ICOL,L)
A(ICOL,L)=DUM
ENDDO
! DO L=1,M
! DUM=B(IROW,L)
! B(IROW,L)=B(ICOL,L)
! B(ICOL,L)=DUM
! ENDDO
!================================
DUM=B(IROW)
B(IROW)=B(ICOL)
B(ICOL)=DUM
!================================
ENDIF
INDXR(I)=IROW
INDXC(I)=ICOL
IF(A(ICOL,ICOL).EQ.0.0D0) THEN
PRINT *,'ERROR! Singular matrix found by GAUSS_JORDAN'
call_writes=.true.
return
! write(6,*) 'Crashed in second loop'
! write(6,*) 'grid point = ',p, ' coolant = ',coolant
! write(6,*) 'gastemperature = ',gastemperature(p)
! STOP
ENDIF
PIVINV=1.0D0/A(ICOL,ICOL)
A(ICOL,ICOL)=1.0D0
DO L=1,N
A(ICOL,L)=A(ICOL,L)*PIVINV
ENDDO
! DO L=1,M
! B(ICOL,L)=B(ICOL,L)*PIVINV
! ENDDO
!=======================================
B(ICOL)=B(ICOL)*PIVINV
!=======================================
DO LL=1,N
IF(LL.NE.ICOL) THEN
DUM=A(LL,ICOL)
A(LL,ICOL)=0.0D0
DO L=1,N
A(LL,L)=A(LL,L)-A(ICOL,L)*DUM
ENDDO
! DO L=1,M
! B(LL,L)=B(LL,L)-B(ICOL,L)*DUM
! ENDDO
!=============================================
B(LL)=B(LL)-B(ICOL)*DUM
!=============================================
ENDIF
ENDDO
ENDDO
DO L=N,1,-1
IF(INDXR(L).NE.INDXC(L)) THEN
DO K=1,N
DUM=A(K,INDXR(L))
A(K,INDXR(L))=A(K,INDXC(L))
A(K,INDXC(L))=DUM
ENDDO
ENDIF
ENDDO
RETURN
END subroutine
!C-----------------------------------------------------------------------
!C Standard Gauss-Jordon linear equation solver from Numerical Recipes
!C A(N,N) is an input matrix stored in an array of dimensions NPxNP
!C B(N,M) is an input matrix containing the M right-hand side vectors
!C stored in an array of dimensions NPxMPP
!C
!C On output, A(N,N) is replaced by its matrix inverse and B(N,M)
!C is replaced by the corresponding set of solution vectors
!C
!C Note: set NMAX to the maximum possible dimension (NLEVEL)
!C-----------------------------------------------------------------------
! SUBROUTINE GAUSS_JORDAN(A,N,NP,B,M,MPP,coolant)
! SUBROUTINE GAUSS_JORDAN_writes(A,N,NP,B,coolant,ill)
SUBROUTINE GAUSS_JORDAN_writes(A,N,NP,B,ill)
use definitions
use healpix_types
use maincode_module, only : gastemperature,p,iteration
IMPLICIT NONE
integer(kind=i4b), intent(in) :: ill!,coolant
INTEGER(kind=i4b):: I,J,K,L,LL,IROW,ICOL
INTEGER(kind=i4b), intent(in):: N,NP!,M,MPP
integer(kind=i4b), PARAMETER :: NMAX=100
INTEGER(kind=i4b):: IPIV(1:NMAX),INDXR(1:NMAX),INDXC(1:NMAX)
real(kind=dp), intent(inout) :: A(1:NP,1:NP)
real(kind=dp), intent(inout) :: B(1:NP)!,1:MPP)
real(kind=dp) :: BIG,DUM,PIVINV
write(6,*) 'b'
do i=1,np
write(6,*) b(i),i
enddo
write(6,*) 'a'
do i=1,np
do j=1,np
write(6,*) a(i,j)
enddo
enddo
ICOL=0
IROW=0
IPIV=0
DO I=1,N
BIG=0.0D0
DO J=1,N
IF(IPIV(J).NE.1) THEN
DO K=1,N
IF(IPIV(K).EQ.0) THEN
IF(ABS(A(J,K)).GE.BIG) THEN
BIG=ABS(A(J,K))
IROW=J
ICOL=K
ENDIF !ABS(A
ELSE IF(IPIV(K).GT.1) THEN
PRINT *,'ERROR! Singular matrix in GAUSS_JORDAN'
write(6,*) 'Crashed in first loop'
! write(6,*) 'grid point = ',p, ' coolant = ',coolant
write(6,*) 'gastemperature = ',gastemperature(p)
STOP
ENDIF !IPIV(K).EQ.0
ENDDO !K=1,N
ENDIF !IPIV(J).NE.1
ENDDO !J=1,N
IPIV(ICOL)=IPIV(ICOL)+1
IF(IROW.NE.ICOL) THEN
DO L=1,N
DUM=A(IROW,L)
A(IROW,L)=A(ICOL,L)
A(ICOL,L)=DUM
ENDDO !L=1,N
! DO L=1,M
! DUM=B(IROW,L)
! B(IROW,L)=B(ICOL,L)
! B(ICOL,L)=DUM
! ENDDO
!================================
DUM=B(IROW)
if (i.eq.ill) write(6,*) 'dum=',dum,'A'
!write(6,*) 'DUM=',DUM
B(IROW)=B(ICOL)
if (i.eq.ill) write(6,*) 'b(',irow,')=',b(irow),'B'
!write(6,*) 'irow=',irow
!write(6,*) 'B(irow)=',b(irow)
B(ICOL)=DUM
if (i.eq.ill) write(6,*) 'b(',icol,')=',b(icol),'C'
!write(6,*) 'icol=',icol
!write(6,*) 'b(icol)=',b(icol)
!================================
ENDIF !IROW.NE.ICOL
INDXR(I)=IROW
INDXC(I)=ICOL
IF(A(ICOL,ICOL).EQ.0.0D0) THEN
PRINT *,'ERROR! Singular matrix found by GAUSS_JORDAN'
write(6,*) 'Crashed in second loop'
! write(6,*) 'grid point = ',p, ' coolant = ',coolant
write(6,*) 'gastemperature = ',gastemperature(p)
STOP
ENDIF
PIVINV=1.0D0/A(ICOL,ICOL)
A(ICOL,ICOL)=1.0D0
DO L=1,N
A(ICOL,L)=A(ICOL,L)*PIVINV
ENDDO
! DO L=1,M
! B(ICOL,L)=B(ICOL,L)*PIVINV
! ENDDO
!=======================================
if (i.eq.ill) write(6,*) b(icol),pivinv,'D'
B(ICOL)=B(ICOL)*PIVINV
if (i.eq.ill) write(6,*) b(icol),'E'
!write(6,*) 'pivinv=',pivinv
!write(6,*) 'b(icol)=',b(icol)
!=======================================
DO LL=1,N
IF(LL.NE.ICOL) THEN
DUM=A(LL,ICOL)
A(LL,ICOL)=0.0D0
DO L=1,N
A(LL,L)=A(LL,L)-A(ICOL,L)*DUM
ENDDO
! DO L=1,M
! B(LL,L)=B(LL,L)-B(ICOL,L)*DUM
! ENDDO
!=============================================
if (i.eq.ill) then
write(6,*) 'll=',ll,'F'
write(6,*) 'b(ll)=',b(ll),'G'
write(6,*) 'b(icol)=',b(icol),'H'
write(6,*) 'dum=',dum,'I'
endif
B(LL)=B(LL)-B(ICOL)*DUM
if (i.eq.ill) write(6,*) 'b(ll) after=',b(ll),'J'
!=============================================
ENDIF !LL.NE.ICOL
ENDDO !LL=1,N
ENDDO ! I=1,N
DO L=N,1,-1
IF(INDXR(L).NE.INDXC(L)) THEN
DO K=1,N
DUM=A(K,INDXR(L))
A(K,INDXR(L))=A(K,INDXC(L))
A(K,INDXC(L))=DUM
ENDDO !K=1,N
ENDIF !INDXR(L).NE.INDXC(L)
ENDDO !L=N,1,-1
do i=1,n
write(6,*) b(i),i
enddo
RETURN
END subroutine