-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotection2.F90
249 lines (223 loc) · 10.2 KB
/
protection2.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
module protection
! Module for Capreole
! Author: Garrelt Mellema
! Date: 2006-12-30
!
! This module contains the routines related to the negative
! pressure / density / total energy correction
! Memory saving version
use precision, only:dp
use sizes, only: mbc,neq, neuler, RHO, EN
use mesh, only: meshx, meshy, meshz, sx,ex,sy,ey,sz,ez
use hydro, only: state, pressr, set_state_pointer
use times, only: time
use abundances, only: mu
use atomic, only: boltzm, gamma1
use geometry, only: presfunc
implicit none
private
public:: presprot
contains
!========================================================================
subroutine presprot(inegative,icall,newold)
! This routine protects the pressure, density, and energy density
! from becoming negative.
! We do this in two stages, if 1) fails, do 2).
! 1) diffuse
! 2) setting a minimal temperature tmin
! the diffusion coefficient used in stage 1)
real(kind=dp),parameter :: eta=0.05d0
! the minimum temperature applied in stage 2)
real(kind=dp),parameter :: tmin=1.0d1
integer,intent(out) :: inegative ! control integer; /= 0 if fixes failed
integer,intent(in) :: icall ! call id (to distinguish between different
! calls to this subroutine)
integer,intent(in) :: newold
integer :: i,j,k,ieq
real(kind=dp) :: pnew
integer :: imin,jmin,iplus,jplus,kmin,kplus
integer :: ierror
integer :: problem_counter, nproblem
! A structure to contain the position and diffusive
! fluxes for a problem
type problem
integer,dimension(3) ::position
real(kind=dp),dimension(2,3,neq) :: dflux
end type problem
! Not more than 1000 problems allowed
type(problem),dimension(1000) :: problem_list
! Point state to appropriate array
state => set_state_pointer(newold)
inegative=0
problem_counter=0
do k=sz-1,ez+1
do j=sy-1,ey+1
do i=sx-1,ex+1
! Check for negative pressure/density/energy
if (pressr(i,j,k) <= 0.0.or.state(i,j,k,RHO) <= 0.0.or. &
state(i,j,k,EN) <= 0.0) then
! Report to log file
write(30,"(A,1PE10.3,2(2X,E10.3))") &
"Negative pressure/density/energy: ", &
pressr(i,j,k),state(i,j,k,RHO),state(i,j,k,EN)
write(30,"(A,3(I4,X),A,1PE10.3)") " at ", &
i,j,k," time = ",time
write(30,*) "call ",icall
call flush(30)
! Set a control variable
problem_counter=problem_counter+1
if (problem_counter < 1001) then
problem_list(problem_counter)%position=(/ i, j, k /)
! Pressure fix 1: diffuse with four neighbours
! diffusion parameter is eta (used below)
! To keep things conservative, express everything
! as fluxes
imin=max(i-1,1) ! diffusive flux at edge 1 is zero
iplus=min(i+1,meshx) ! diffusive flux at edge 1 is zero
jmin=max(j-1,1)
jplus=min(j+1,meshy)
kmin=max(k-1,1)
kplus=min(k+1,meshz)
! Only set diffuse flux if the neighbouring cell has
! enough positive pressure itself, or if we are
! correcting a negative density or energy.
if (-(pressr(imin,j,k)/pressr(i,j,k)) > eta .or. &
-(state(imin,j,k,RHO)/state(i,j,k,RHO)) > eta) then
!.or. &
! pressr(i,j,k) > 0.0d0 ) then
problem_list(problem_counter)%dflux(1,1,:)= &
(state(imin,j,k,:)-state(i,j,k,:))
else
problem_list(problem_counter)%dflux(1,1,:)=0.0
endif
if (-(pressr(iplus,j,k)/pressr(i,j,k)) > eta .or. &
-(state(iplus,j,k,RHO)/state(i,j,k,RHO)) > eta) then
!.or. &
! pressr(i,j,k) > 0.0d0 ) then
problem_list(problem_counter)%dflux(2,1,:)= &
(-state(iplus,j,k,:)+state(i,j,k,:))
else
problem_list(problem_counter)%dflux(2,1,:)=0.0
endif
if (-(pressr(i,jmin,k)/pressr(i,j,k)) > eta .or. &
-(state(i,jmin,k,RHO)/state(i,j,k,RHO)) > eta) then
!.or. &
! pressr(i,j,k) > 0.0d0 ) then
problem_list(problem_counter)%dflux(1,2,:)= &
(state(i,jmin,k,:)-state(i,j,k,:))
else
problem_list(problem_counter)%dflux(1,2,:)=0.0
endif
if (-(pressr(i,jplus,k)/pressr(i,j,k)) > eta .or. &
-(state(i,jplus,k,RHO)/state(i,j,k,RHO)) > eta) then
!.or. &
! pressr(i,j,k) > 0.0d0 ) then
problem_list(problem_counter)%dflux(2,2,:)= &
(-state(i,jplus,k,:)+state(i,j,k,:))
else
problem_list(problem_counter)%dflux(2,2,:)=0.0
endif
if (-(pressr(i,j,kmin)/pressr(i,j,k)) > eta .or. &
-(state(i,j,kmin,RHO)/state(i,j,k,RHO)) > eta) then
!.or. &
! pressr(i,j,k) > 0.0d0 ) then
problem_list(problem_counter)%dflux(1,3,:)= &
(state(i,j,kmin,:)-state(i,j,k,:))
else
problem_list(problem_counter)%dflux(1,3,:)=0.0
endif
if (-(pressr(i,j,kplus)/pressr(i,j,k)) > eta .or. &
-(state(i,j,kplus,RHO)/state(i,j,k,RHO)) > eta) then
!.or. &
! pressr(i,j,k) > 0.0d0 ) then
problem_list(problem_counter)%dflux(2,3,:)= &
(-state(i,j,kplus,:)+state(i,j,k,:))
else
problem_list(problem_counter)%dflux(2,3,:)=0.0
endif
else
write(30,*) "ERROR: Too many problems!"
endif
endif
enddo
enddo
enddo
do nproblem=1,problem_counter
!write(30,*)
! Apply fluxes
i=problem_list(nproblem)%position(1)
j=problem_list(nproblem)%position(2)
k=problem_list(nproblem)%position(3)
!write(30,"(A,5(1pe10.3))") "Old: ",state(i,j,k,1:neuler)
!write(30,"(A,5(1pe10.3))") " ",state(i-1,j,k,1:neuler)
!write(30,"(A,5(1pe10.3))") " ",state(i+1,j,k,1:neuler)
!write(30,"(A,5(1pe10.3))") " ",state(i,j-1,k,1:neuler)
!write(30,"(A,5(1pe10.3))") " ",state(i,j+1,k,1:neuler)
!write(30,"(A,5(1pe10.3))") " ",state(i,j,k-1,1:neuler)
!write(30,"(A,5(1pe10.3))") " ",state(i,j,k+1,1:neuler)
state(i,j,k,:)=state(i,j,k,:)+eta* &
(problem_list(nproblem)%dflux(1,1,:)- &
problem_list(nproblem)%dflux(2,1,:) + &
problem_list(nproblem)%dflux(1,2,:)- &
problem_list(nproblem)%dflux(2,2,:) + &
problem_list(nproblem)%dflux(1,3,:)- &
problem_list(nproblem)%dflux(2,3,:))
if (i > 1) state(i-1,j,k,:)=state(i-1,j,k,:)-eta* &
problem_list(nproblem)%dflux(1,1,:)
if (i < meshx) state(i+1,j,k,:)=state(i+1,j,k,:)+eta* &
problem_list(nproblem)%dflux(2,1,:)
if (j > 1) state(i,j-1,k,:)=state(i,j-1,k,:)-eta* &
problem_list(nproblem)%dflux(1,2,:)
if (j < meshy) state(i,j+1,k,:)=state(i,j+1,k,:)+eta* &
problem_list(nproblem)%dflux(2,2,:)
if (k > 1) state(i,j,k-1,:)=state(i,j,k-1,:)-eta* &
problem_list(nproblem)%dflux(1,3,:)
if (k < meshz) state(i,j,k+1,:)=state(i,j,k+1,:)+eta* &
problem_list(nproblem)%dflux(2,3,:)
!write(30,"(A,5(1pe10.3))") "New: ",state(i,j,k,1:neuler)
!write(30,"(A,5(1pe10.3))") " ",state(i-1,j,k,1:neuler)
!write(30,"(A,5(1pe10.3))") " ",state(i+1,j,k,1:neuler)
!write(30,"(A,5(1pe10.3))") " ",state(i,j-1,k,1:neuler)
!write(30,"(A,5(1pe10.3))") " ",state(i,j+1,k,1:neuler)
!write(30,"(A,5(1pe10.3))") " ",state(i,j,k-1,1:neuler)
!write(30,"(A,5(1pe10.3))") " ",state(i,j,k+1,1:neuler)
enddo
if (problem_counter > 0) then
! Recalculate the pressure after the diffusion
! the presfunc routine needs to be supplied
call presfunc(sx,ex,sy,ey,sz,ez,newold,ierror)
do k=sz,ez
do j=sy,ey
do i=sx,ex
! Check if the pressure is still negative
if (pressr(i,j,k) <= 0.0) then ! check result of fix 1
write(30,*) "Still Negative pressure: ", &
pressr(i,j,k)," at ",i,j,k
call flush(30)
! Pressure fix 2: set temperature to minimum value
pnew=state(i,j,k,RHO)*boltzm*tmin/mu
!pnew=temper2pressr(tmin,rho2n(state(i,j,k,RHO)), &
! electrondens(rho2n(state(i,j,k,RHO)), &
! state(i,j,k,XHI:XHII))
state(i,j,k,EN)=state(i,j,k,EN)+(pnew-pressr(i,j,k))/gamma1
pressr(i,j,k)=pnew
endif
! Check for negative densities and energies
! These are fatal. inegative is used to communicate
! this to the calling program.
if (state(i,j,k,RHO) <= 0.0) then
write(30,*) "Still negative density: ", &
state(i,j,k,RHO)," at ",i,j,k
inegative=1
endif
if (state(i,j,k,EN) <= 0.0) then
write(30,*) "Still negative energy: ", &
state(i,j,k,EN)," at ",i,j,k
inegative=2
endif
enddo
enddo
enddo
endif
end subroutine presprot
end module protection