-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlof.f90
353 lines (287 loc) · 10.6 KB
/
lof.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
module oddeven
! Module for Capreole (2D)
! Author: Garrelt Mellema
! Date: 2003-08-26
!
! This module contains the routines related to the correction
! of the odd-even decoupling / carbuncle phenomenon
use precision
use scaling
use sizes
use grid
use coords
use atomic
use geometry
private
! Shock detection parameter
real(kind=dp),parameter,private :: shockDetectFactor = 0.5_dp
! The diffusion coefficient used
real(kind=dp),parameter,private :: eta=0.05_dp
integer,private :: i,j,k,ieq,m
real(kind=dp),private :: deltaPressure
public :: odd_even
! private :: set_odd_even_x,set_odd_even_y,apply_odd_even_x,apply_odd_even_y
! private :: detect_shock_x,detect_shock_y
contains
!========================================================================
subroutine odd_even (state,pressr,vol,time,action)
! This routine handles the odd-even fix interface with the
! integration routine. The actual work is done in the set and
! apply routines for the different coordinate directions.
real(kind=dp),dimension(sx-mbc:ex+mbc,sy-mbc:ey+mbc,neq), &
intent(inout) :: state
real(kind=dp),dimension(sx-mbc:ex+mbc,sy-mbc:ey+mbc), &
intent(inout) :: pressr
real(kind=dp),dimension(sx-mbc:ex+mbc,sy-mbc:ey+mbc), &
intent(in) :: vol
real(kind=dp),intent(in) :: time
integer :: action ! what to do:
! 1: fix x-direction
! 2: fix y-direction
! 0: fix all directions
! other values: do nothing
! Flag for shock detection
! ( exported via state(neq) )
integer,dimension(sx-mbc:ex+mbc,sy-mbc:ey+mbc) :: flag
! Diffusive fluxes
real(kind=dp),dimension(sx-mbc:ex+mbc,sy-mbc:ey+mbc,neq) :: fdiff
real(kind=dp),dimension(sx-mbc:ex+mbc,sy-mbc:ey+mbc,neq) :: gdiff
! Counters
integer :: nrOfXCorrections,nrOfYCorrections
! Figure out what to do.
select case (action)
case (0)
call set_odd_even_x ()
state(sx:ex,sy:ey,neq)=state(sx:ex,sy:ey,neq)+real(flag(sx:ex,sy:ey),dp)
call set_odd_even_y ()
state(sx:ex,sy:ey,neq)=state(sx:ex,sy:ey,neq)+real(flag(sx:ex,sy:ey),dp)
call apply_odd_even_x ()
call apply_odd_even_y ()
case (1)
call set_odd_even_x ()
call apply_odd_even_x ()
state(sx:ex,sy:ey,neq)=state(sx:ex,sy:ey,neq)+real(flag(sx:ex,sy:ey),dp)
case (2)
call set_odd_even_y ()
call apply_odd_even_y ()
state(sx:ex,sy:ey,neq)=state(sx:ex,sy:ey,neq)+real(flag(sx:ex,sy:ey),dp)
case default
! Do nothing
end select
contains
subroutine set_odd_even_x ()
! This routine find shocks in the perpendicular direction,
! then looks for density oscillations parallel to those shocks,
! and calculates a diffusive flux if found.
integer :: nrOfFlags,cellCount
integer :: imin,iplus
! Reset flags
flag(:,:) = 0
cellCount=0
nrOfXCorrections=0
! Reset diffusive flux
fdiff(:,:,:)=0.0_dp
! Find the shocks in y direction, set flag to 1.
nrOfFlags=detect_shock_y (1)
! Test for odd-even pattern.
! Use 4-point up and down pattern for this
do j=sy,ey
do i=sx,ex
cellCount = 0
do k=-2,1
if(flag(i+k,j) == 1 .or. flag(i+k,j) == 2) &
cellCount = cellCount + 1
enddo
if(cellCount == 4) then
if(state(i-2,j,1) > state(i-1,j,1).and. &
state(i-1,j,1) < state(i ,j,1).and. &
state(i ,j,1) > state(i+1,j,1)) &
flag(i,j) = 2
if(state(i-2,j,1) < state(i-1,j,1).and. &
state(i-1,j,1) > state(i ,j,1).and. &
state(i ,j,1) < state(i+1,j,1)) &
flag(i,j) = 2
endif
enddo
enddo
do j=sy,ey
do i=sx,ex
if( flag(i,j) == 2 ) then
nrOfXCorrections = nrOfXCorrections + 1
imin=max(i-1,1) ! diffusive flux at edge 1 is zero
iplus=min(i+1,meshx) ! diffusive flux at edge 1 is zero
do ieq=1,neq
fdiff(i,j,ieq)=(state(imin,j,ieq)-state(i,j,ieq))
fdiff(i+1,j,ieq)=(-state(iplus,j,ieq)+state(i,j,ieq))
enddo
end if
end do
enddo
end subroutine set_odd_even_x
!--------------------------------------------------------------------------
subroutine apply_odd_even_x ()
! Apply the diffusive flux
if ( nrOfXCorrections > 0 ) then
do ieq=1,neq
do j=sy,ey
do i=sx,ex
state(i,j,ieq)=state(i,j,ieq) + &
eta*(fdiff(i,j,ieq)-fdiff(i+1,j,ieq))
enddo
enddo
enddo
end if
end subroutine apply_odd_even_x
!--------------------------------------------------------------------------
subroutine set_odd_even_y ()
! This routine find shocks in the perpendicular direction,
! then looks for density oscillations parallel to those shocks,
! and calculates a diffusive flux if found.
integer :: nrOfFlags,cellCount
integer :: jmin,jplus
! Reset flags
flag(:,:) = 0
cellCount=0
nrOfYCorrections=0
! Reset diffusive flux
gdiff(:,:,:)=0.0_dp
! Find the shocks in x direction, set flag to 3
nrOfFlags=detect_shock_x (3)
! Test for odd-even pattern.
! Use 4-point up and down pattern for this
do j=sy,ey
do i=sx,ex
cellCount = 0
do k=-2,1
if(flag(i,j+k) == 3 .or. flag(i,j+k) == 4) &
cellCount = cellCount + 1
enddo
if(cellCount == 4) then
if(state(i,j-2,1) > state(i,j-1,1).and. &
state(i,j-1,1) < state(i,j ,1).and. &
state(i,j ,1) > state(i,j+1,1)) &
flag(i,j) = 4
if(state(i,j-2,1) < state(i,j-1,1).and. &
state(i,j-1,1) > state(i,j ,1).and. &
state(i,j ,1) < state(i,j+1,1)) &
flag(i,j) = 4
endif
enddo
enddo
do j=sy,ey
do i=sx,ex
if(flag(i,j) == 4) then
nrOfYCorrections = nrOfYCorrections + 1
jmin=max(j-1,1) ! diffusive flux at edge 1 is zero
jplus=min(j+1,meshy) ! diffusive flux at edge 1 is zero
!if (j == 169) write(*,*) i,j, nrOfYCorrections
do ieq=1,neq
gdiff(i,j,ieq)=(state(i,jmin,ieq)-state(i,j,ieq))
gdiff(i,j+1,ieq)=(-state(i,jplus,ieq)+state(i,j,ieq))
end do
end if
end do
end do
end subroutine set_odd_even_y
!--------------------------------------------------------------------------
subroutine apply_odd_even_y ()
! Apply the diffusive flux
if (nrOfYCorrections > 0) then
do ieq=1,neq
do j=sy,ey
do i=sx,ex
state(i,j,ieq)=state(i,j,ieq) + &
eta*(gdiff(i,j,ieq)-gdiff(i,j+1,ieq))
enddo
enddo
enddo
end if
end subroutine apply_odd_even_y
!--------------------------------------------------------------------------
function detect_shock_x (marker) result(shock_x)
! Detects shocks in x-direction and flags post-shock region with marker
! This is done by looking at the pressure jumps.
! Output: number of cells flagged
integer :: shock_x
integer,intent(in) :: marker
integer :: i1,i2,i3,i4,i5
shock_x=0
do j=sy,ey
do i=sx,ex
deltaPressure = (pressr(i+1,j)-pressr(i,j)) / &
max(pressr(i+1,j),pressr(i,j))
! Right going shock
if(-(deltaPressure) > shockDetectFactor) then
i4=max(i-4,sx-mbc)
i3=max(i-3,sx-mbc)
i2=max(i-2,sx-mbc)
i1=max(i-1,sx-mbc)
flag(i4,j) = marker
flag(i3,j) = marker
flag(i2,j) = marker
flag(i1,j) = marker
flag(i, j) = marker
shock_x = shock_x + 5
endif
! Left going shock
if((deltaPressure) > shockDetectFactor) then
i5=min(i+5,ex+mbc)
i4=min(i+4,ex+mbc)
i3=min(i+3,ex+mbc)
i2=min(i+2,ex+mbc)
i1=min(i+1,ex+mbc)
flag(i5,j) = marker
flag(i4,j) = marker
flag(i3,j) = marker
flag(i2,j) = marker
flag(i1,j) = marker
shock_x = shock_x + 5
endif
enddo
enddo
end function detect_shock_x
!--------------------------------------------------------------------------
function detect_shock_y (marker) result(shock_y)
! Detects shocks in y-direction and flags post-shock region with marker
! This is done by looking at the pressure jumps.
! Output: number of cells flagged
integer :: shock_y
integer,intent(in) :: marker
integer :: j1,j2,j3,j4,j5
shock_y=0
do j=sy,ey
do i=sx,ex
deltaPressure = (pressr(i,j+1)-pressr(i,j)) / &
max(pressr(i,j+1),pressr(i,j))
! Right going shock
if(-(deltaPressure) > shockDetectFactor) then
j4=max(j-4,sy-mbc)
j3=max(j-3,sy-mbc)
j2=max(j-2,sy-mbc)
j1=max(j-1,sy-mbc)
flag(i,j4) = marker
flag(i,j3) = marker
flag(i,j2) = marker
flag(i,j1) = marker
flag(i, j) = marker
shock_y = shock_y + 5
endif
! Left going shock
if((deltaPressure) > shockDetectFactor) then
j5=min(j+5,ey+mbc)
j4=min(j+4,ey+mbc)
j3=min(j+3,ey+mbc)
j2=min(j+2,ey+mbc)
j1=min(j+1,ey+mbc)
flag(i,j5) = marker
flag(i,j4) = marker
flag(i,j3) = marker
flag(i,j2) = marker
flag(i,j1) = marker
shock_y = shock_y + 5
endif
enddo
enddo
end function detect_shock_y
end subroutine odd_even
end module oddeven