-
Notifications
You must be signed in to change notification settings - Fork 1
/
convdiff.f90
400 lines (310 loc) · 9.69 KB
/
convdiff.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
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
389
390
391
392
393
394
395
396
397
398
399
400
module convdiff
implicit none
integer,parameter :: minsize=5
real*8 :: k
real*8 :: c
real*8 :: h
real*8 :: phi0
real*8 :: phiL
contains
!===============================================================
subroutine findupwindconvflux(cL,cR,uL,uR,flux)
real*8,intent(in) :: cL,cR
real*8,intent(in) :: uL,uR
real*8,intent(out) :: flux
real*8 :: c_half
c_half=0.5*(cL+cR)
if(c_half .ge. 0.d0) then
flux=c_half*uL
else
flux=c_half*uR
endif
end subroutine findupwindconvflux
!===============================================================
subroutine finddiffusiveflux(DL,DR,uL,uR,dx,flux)
real*8,intent(in) :: DL,DR
real*8,intent(in) :: uL,uR
real*8,intent(in) :: dx
real*8,intent(out) :: flux
real*8 :: D_half
D_half = 0.5*(DL+DR)
flux = D_half*(uR-uL)/dx
end subroutine finddiffusiveflux
!===============================================================
subroutine findAX(AX,X,timederivfactor,vel,dcoeff,reac,dirc_bc_flags,&
flux_bc_flags,dircvals,fluxvals,dx,dt,n)
integer, intent(in) :: n
real*8, intent(inout) :: AX(n)
real*8, intent(in) :: X(n)
real*8,intent(in) :: vel(n),dcoeff(n),reac(n)
real*8,intent(in) :: dx,dt
logical,intent(in) :: dirc_bc_flags(2),flux_bc_flags(2)
real*8, intent(in) :: dircvals(2),fluxvals(2)
real*8, intent(in) :: timederivfactor
integer :: i
real*8 :: flux
real*8 :: dx2
dx2 = dx*dx
AX(:) = timederivfactor*X(:)/dt
!is the volume half of dx at the boundary?
!AX(1) = 0.5*timederivfactor*X(1)/dt
!AX(n) = 0.5*timederivfactor*X(n)/dt
!convection and diffusion terms
do i=1,n-1
call findupwindconvflux(vel(i),vel(i+1),X(i),X(i+1),flux)
AX(i) = AX(i) + flux/dx
AX(i+1) = AX(i+1) - flux/dx
call finddiffusiveflux(dcoeff(i),dcoeff(i+1),X(i),X(i+1),dx,flux)
AX(i) = AX(i) - flux/dx
AX(i+1) = AX(i+1) + flux/dx
enddo
!print *,"AX inside findAX:",AX
!reaction term
do i=1,n
AX(i) = AX(i) - reac(i)*X(i)
enddo
!boundary conditions
if(dirc_bc_flags(1) .eqv. .true.) then
AX(1)=X(1)
endif
if(dirc_bc_flags(2) .eqv. .true.) then
AX(n)=X(n)
endif
!note: for flux bc, the terms go into b and not AX
!if the volume is half of dx at the boundaries the reac(i)*X(i)
!should be 0.5*reac(i)*X(i)
end subroutine findAX
!===============================================================
subroutine findrhs(b,xold,timederivfactor,&
source,dirc_bc_flags,flux_bc_flags,dircvals,&
fluxvals,dx,dt,n)
integer, intent(in) :: n
real*8, intent(inout) :: b(n)
real*8, intent(in) :: xold(n),source(n)
logical, intent(in) :: dirc_bc_flags(2),flux_bc_flags(2)
real*8, intent(in) :: dircvals(2),fluxvals(2)
real*8, intent(in) :: dx,dt
real*8, intent(in) :: timederivfactor
integer :: i
do i=1,n
b(i) = timederivfactor*xold(i)/dt + source(i)
enddo
!is the volume half of dx at the boundary?
!b(1) = 0.5*(timederivfactor*xold(1)/dt + source(1))
!b(n) = 0.5*(timederivfactor*xold(n)/dt + source(n))
if(dirc_bc_flags(1) .eqv. .true.) then
b(1) = dircvals(1)
endif
if(dirc_bc_flags(2) .eqv. .true.) then
b(n) = dircvals(2)
endif
if(flux_bc_flags(1) .eqv. .true.) then
b(1) = b(1) + fluxvals(1)/(dx)
endif
if(flux_bc_flags(2) .eqv. .true.) then
b(n) = b(n) - fluxvals(2)/(dx)
endif
!if the volume is half of dx then the fluxvals should be
!divided by 0.5*dx
end subroutine findrhs
!===============================================================
subroutine noprecond(MinvX,X,timederivfactor,vel,dcoeff,reac,dirc_bc_flags,&
flux_bc_flags,dircvals,fluxvals,&
dx,dt,n)
integer, intent(in) :: n
real*8, intent(inout) :: MinvX(n)
real*8, intent(in) :: X(n)
logical, intent(in) :: dirc_bc_flags(2),flux_bc_flags(2)
real*8, intent(in) :: dircvals(2),fluxvals(2)
real*8,intent(in) :: vel(n),dcoeff(n),reac(n)
real*8,intent(in) :: dx,dt
real*8,intent(in) :: timederivfactor
MinvX = X
end subroutine noprecond
!===============================================================
subroutine gauss_seidel_smoothing(res,b,X,timederivfactor,vel,dcoeff,reac,&
dirc_bc_flags,flux_bc_flags,dircvals,&
fluxvals,dx,dt,n,maxiter)
integer, intent(in) :: n
real*8, intent(in) :: b(n)
real*8, intent(inout) :: X(n)
real*8, intent(inout) :: res(n)
integer, intent(in) :: maxiter
logical, intent(in) :: dirc_bc_flags(2),flux_bc_flags(2)
real*8, intent(in) :: dircvals(2),fluxvals(2)
real*8,intent(in) :: vel(n),dcoeff(n),reac(n)
real*8,intent(in) :: dx,dt
real*8,intent(in) :: timederivfactor
integer :: i,it
real*8 :: diag
real*8 :: cL,cR,chalf
real*8 :: dL,dR,dhalf
real*8 :: offdiag
real*8 :: AX(n)
real*8 :: dx2
dx2 = dx*dx
!gauss seidel iterations
do it=1,maxiter
do i=1,n
diag = -reac(i) + timederivfactor*1.d0/dt
!is the volume half of dx at the boundary?
!if((i .eq. 1) .or. (i .eq. n)) then
! diag = diag
!else
! diag = diag
!endif
offdiag=0.d0
!right face
if(i .lt. n) then
!convection term
cL = vel(i)
cR = vel(i+1)
chalf = 0.5*(cL+cR)
if(chalf .ge. 0) then
diag = diag + chalf/dx
else
offdiag = offdiag + chalf*X(i+1)/dx
endif
!diffusion term
dL = dcoeff(i)
dR = dcoeff(i+1)
dhalf = 0.5*(dL + dR)
diag = diag + dhalf/dx2
offdiag = offdiag - X(i+1)*dhalf/dx2
endif
!left face
if(i .gt. 1) then
!convection term
cL = vel(i-1)
cR = vel(i)
chalf = 0.5*(cL+cR)
if(chalf .ge. 0) then
offdiag = offdiag - chalf*X(i-1)/dx
else
diag = diag - chalf/dx
endif
!diffusion term
dL = dcoeff(i-1)
dR = dcoeff(i)
dhalf = 0.5*(dL+dR)
diag = diag + dhalf/dx2
offdiag = offdiag - X(i-1)*dhalf/dx2
endif
if(i .eq. 1) then
if(dirc_bc_flags(1) .eqv. .true.) then
diag = 1.d0
offdiag = 0.d0
endif
endif
if(i .eq. n) then
if(dirc_bc_flags(2) .eqv. .true.) then
diag = 1.d0
offdiag = 0.d0
endif
endif
X(i) = (b(i)-offdiag)/diag
enddo
enddo
call findAX(AX,X,timederivfactor,vel,dcoeff,reac,dirc_bc_flags,&
flux_bc_flags,dircvals,fluxvals,dx,dt,n)
res = b - AX
end subroutine gauss_seidel_smoothing
!===============================================================
subroutine restriction(Xh,X2h,n2h)
integer, intent(in) :: n2h
real*8, intent(inout) :: Xh(2*n2h-1)
real*8, intent(inout) :: X2h(n2h)
integer :: i
X2h(1) = Xh(1)
X2h(n2h) = Xh(2*n2h-1)
do i=2,n2h-1
X2h(i)=0.25*(Xh(2*i-2) + 2.0*Xh(2*i-1) + Xh(2*i))
enddo
end subroutine restriction
!===============================================================
subroutine prolong(Xh,X2h,n2h)
integer, intent(in) :: n2h
real*8, intent(inout) :: Xh(2*n2h-1)
real*8, intent(inout) :: X2h(n2h)
integer :: i
do i=1,n2h-1
Xh(2*i-1) = X2h(i)
Xh(2*i) = 0.5*(X2h(i) + X2h(i+1))
enddo
Xh(2*n2h-1) = X2h(n2h)
end subroutine prolong
!===============================================================
recursive subroutine dovcycle(X,b,timederivfactor,vel,dcoeff,reac,&
dirc_bc_flags,flux_bc_flags,dircvals,&
fluxvals,dx,dt,n)
integer, intent(in) :: n
real*8, intent(inout) :: X(n)
real*8, intent(inout) :: b(n)
logical, intent(in) :: dirc_bc_flags(2),flux_bc_flags(2)
real*8, intent(in) :: dircvals(2),fluxvals(2)
real*8, intent(in) :: vel(n),dcoeff(n),reac(n)
real*8 ,intent(in) :: dx,dt
real*8 ,intent(in) :: timederivfactor
real*8 :: resh(n)
real*8 :: eh(n)
real*8 :: velh(n)
real*8 :: dcoeffh(n)
real*8 :: reach(n)
real*8 :: res2h(n/2+1)
real*8 :: e2h(n/2+1)
real*8 :: vel2h(n/2+1)
real*8 :: dcoeff2h(n/2+1)
real*8 :: reac2h(n/2+1)
velh = vel
dcoeffh = dcoeff
reach = reac
if(n .le. minsize) then
call gauss_seidel_smoothing(resh,b,X,timederivfactor,velh,dcoeffh,reach,&
dirc_bc_flags,flux_bc_flags,dircvals,&
fluxvals,dx,dt,n,1000)
else
!initial smoothing
call gauss_seidel_smoothing(resh,b,X,timederivfactor,velh,dcoeffh,reach,&
dirc_bc_flags,flux_bc_flags,dircvals,&
fluxvals,dx,dt,n,4)
!restriction of residual from fine to coarse grid
call restriction(resh,res2h,n/2+1)
call restriction(reach,reac2h,n/2+1)
call restriction(dcoeffh,dcoeff2h,n/2+1)
call restriction(velh,vel2h,n/2+1)
e2h = 0.d0
call dovcycle(e2h,res2h,timederivfactor,vel2h,dcoeff2h,reac2h,&
dirc_bc_flags,flux_bc_flags,dircvals,&
fluxvals,2*dx,dt,n/2+1)
!prolong error from coarse to fine grid
call prolong(eh,e2h,n/2+1)
!update
X(:) = X(:) + eh(:)
!post smooth
call gauss_seidel_smoothing(resh,b,X,timederivfactor,velh,dcoeffh,reach,&
dirc_bc_flags,flux_bc_flags,dircvals,&
fluxvals,dx,dt,n,4)
endif
end subroutine dovcycle
!===============================================================
subroutine mgridprecond(MinvX,X,timederivfactor,vel,dcoeff,reac,dirc_bc_flags,&
flux_bc_flags,dircvals,fluxvals,&
dx,dt,n)
integer, intent(in) :: n
real*8, intent(inout) :: MinvX(n)
real*8, intent(inout) :: X(n)
logical, intent(in) :: dirc_bc_flags(2),flux_bc_flags(2)
real*8, intent(in) :: dircvals(2),fluxvals(2)
real*8,intent(in) :: vel(n),dcoeff(n),reac(n)
real*8,intent(in) :: dx,dt
real*8,intent(in) :: timederivfactor
integer :: nvcycles,i
nvcycles=10
do i=1,nvcycles
call dovcycle(MinvX,X,timederivfactor,vel,dcoeff,reac,&
dirc_bc_flags,flux_bc_flags,dircvals,&
fluxvals,dx,dt,n)
enddo
end subroutine mgridprecond
!===============================================================
end module convdiff