Debian Codegraph @

src/exact_shock.f90

raw
1 !-----------------------------------------------------------------
2 !
3 ! This file is (or was) part of SPLASH, a visualisation tool
4 ! for Smoothed Particle Hydrodynamics written by Daniel Price:
5 !
6 ! http://users.monash.edu.au/~dprice/splash
7 !
8 ! SPLASH comes with ABSOLUTELY NO WARRANTY.
9 ! This is free software; and you are welcome to redistribute
10 ! it under the terms of the GNU General Public License
11 ! (see LICENSE file for details) and the provision that
12 ! this notice remains intact. If you modify this file, please
13 ! note section 2a) of the GPLv2 states that:
14 !
15 ! a) You must cause the modified files to carry prominent notices
16 ! stating that you changed the files and the date of any change.
17 !
18 ! Copyright (C) 2005-2015 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 ! ----------------------------------------------------------------------
24 ! compute exact solution for the one dimensional Riemann problem
25 ! (hydrodynamic shock)
26 !
27 ! input parameters are initial left and right states of
28 ! density, pressure and velocity
29 !
30 ! Computes shock profile at time t
31 !
32 ! Calls a separate subroutine to calculate the post-shock pressure
33 ! and velocity (this is the difficult bit).
34 !
35 ! Handles all cases of left and right-going shocks and rarefactions
36 !
37 ! Daniel Price, Institute of Astronomy, Cambridge, 2004
38 ! University of Exeter 2004-2008
39 ! Monash University 2008-
40 !
41 ! [email protected]
42 !-----------------------------------------------------------------------
43 module shock
44 implicit none
45 public :: exact_shock
46 private :: get_pstar, get_pstar_isothermal, f_and_df
47
48 contains
49
50 subroutine exact_shock(iplot,time,gammain,xshock,rho_L,rho_R,p_L,p_R,v_L,v_R,rdust_to_gas,xplot,yplot,ierr)
51 integer, intent(in) :: iplot
52 integer, intent(out) :: ierr
53 real, intent(in) :: time,gammain,xshock
54 real, intent(in) :: rho_L,rho_R,p_L,p_R,v_L,v_R,rdust_to_gas
55 real, dimension(:), intent(in) :: xplot
56 real, dimension(size(xplot)), intent(out) :: yplot
57
58 integer :: i
59 real, dimension(size(xplot)) :: dens, pr, vel
60 real :: cs_L,cs_R, gamfac
61 real :: ppost, vpost, vleft, vright, gamma
62 real :: xzero,xleft,xleftleft,xcontact,xright,xrightright
63 logical :: useisothermal,leftisshock,rightisshock
64
65 gamma = gammain
66 print*,'Plotting exact Riemann solution at t = ',time,' gamma = ',gamma
67 !
68 ! check for errors in input
69 !
70 ierr = 0
71 if (rho_L <= 0. .or. rho_R <= 0.) then
72 print*,'error: rho <= 0 on input : ',rho_L,rho_R
73 ierr = 1
74 return
75 elseif (p_L <= 0. .or. p_R <= 0.) then
76 print*,'error: pr <= 0 on input ',p_L, p_R
77 ierr = 2
78 return
79 endif
80 if (gamma < 1.) then
81 print*,'Error: gamma = ',gamma,' setting to 5/3'
82 gamma = 5./3.
83 endif
84 !
85 ! xzero is the position of the shock at t=0
86 !
87 xzero = xshock
88 !
89 ! define sound speeds to left and right of shock tube
90 !
91 cs_L = sqrt(gamma*p_L/rho_L)
92 cs_R = sqrt(gamma*p_R/rho_R)
93 if (rdust_to_gas > epsilon(rdust_to_gas)) then
94 cs_L = cs_L*sqrt(1./(1.+rdust_to_gas))
95 cs_R = cs_R*sqrt(1./(1.+rdust_to_gas))
96 endif
97 gamfac = (gamma-1.)/(gamma + 1.)
98
99 !------------------------------------------------------------
100 ! find post-shock pressure via the Riemann solver
101 ! (this version also returns the post shock speed vpost
102 ! although this can be calculated from ppost)
103 !------------------------------------------------------------
104
105 if (gamma > 1.0001) then
106 call get_pstar(gamma,p_L,p_R,v_L,v_R,cs_L,cs_R,ppost,vpost)
107 useisothermal = .false.
108 else
109 print*,'using isothermal solver...',p_L/rho_L, p_R/rho_R
110 useisothermal = .true.
111 call get_pstar_isothermal(cs_L*cs_L,v_L,v_R,rho_L,rho_R,ppost,vpost)
112 endif
113
114 !------------------------------------------------------------
115 ! using this, calculate various speeds needed in order to
116 ! reconstruct the shock profile
117 !------------------------------------------------------------
118
119 !
120 ! check whether solutions are shocks or rarefactions
121 !
122 if (ppost > p_L) then
123 leftisshock = .true.
124 else
125 leftisshock = .false.
126 endif
127 if (ppost > p_R) then
128 rightisshock = .true.
129 else
130 rightisshock = .false.
131 endif
132 if (leftisshock) then
133 write(*,"(a)",advance='no') ' left-going wave is a shock; '
134 else
135 write(*,"(a)",advance='no') ' left-going wave is a rarefaction; '
136 endif
137 if (rightisshock) then
138 write(*,"(a)") 'right-going wave is a shock'
139 else
140 write(*,"(a)") 'right-going wave is a rarefaction'
141 endif
142
143 if (rightisshock) then ! right hand wave is a shock
144 !
145 ! speed of the shock front
146 !
147 vright = v_R + cs_R**2*(ppost/p_R - 1.)/(gamma*(vpost-v_R))
148 else ! right hand wave is a rarefaction
149 !
150 ! speed at which the right end of rarefaction fan moves
151 !
152 vright = cs_R + 0.5*(gamma+1.)*vpost - 0.5*(gamma-1.)*v_R
153 endif
154 !
155 ! repeat for left-going wave
156 !
157 if (leftisshock) then
158 vleft = -(v_L + cs_L**2*(ppost/p_L - 1.)/(gamma*(vpost-v_L)))
159 else
160 vleft = cs_L - 0.5*(gamma+1.)*vpost + 0.5*(gamma-1.)*v_L
161 endif
162
163 !-------------------------------------------------------------
164 ! now work out the locations of various features in the shock
165 !-------------------------------------------------------------
166
167 !
168 ! position of left-going shock or back end of left-going rarefaction
169 !
170 xleft = xzero - vleft*time
171 if (leftisshock) then
172 xleftleft = xleft
173 else
174 !
175 ! front end of left-going expansion fan (propagates at sound speed into "left" fluid)
176 !
177 xleftleft = xzero - (cs_L - v_L)*time
178 endif
179 !
180 ! position of the interface between the "left" fluid from the "right" fluid
181 ! (the contact discontinuity)
182 !
183 xcontact = xzero + vpost*time
184
185 !
186 ! position of right-going shock or back end of right-going rarefaction
187 !
188 xright = xzero + vright*time
189 if (rightisshock) then
190 xrightright = xright
191 else
192 !
193 ! right end of right-going expansion fan (propagates at sound speed into "right" fluid)
194 !
195 xrightright = xzero + (cs_R + v_R)*time
196 endif
197
198 !--------------------------------------------------------------
199 ! reconstruct the shock profile for all x
200 !--------------------------------------------------------------
201
202 !--here is a cheap, dirty f90 version for crap compilers
203 do i=1,size(xplot)
204 if (xplot(i) <= xleftleft) then
205 ! undisturbed medium to the left
206 pr(i) = p_L
207 dens(i) = rho_L
208 vel(i) = v_L
209 elseif (xplot(i) < xleft) then
210 if (leftisshock) then
211 pr(i) = ppost
212 dens(i) = rho_L*(gamfac+ppost/p_L)/(1+gamfac*ppost/p_L)
213 ! dens(i) = rho_L*(ppost/p_L)**(1./gamma)
214 vel(i) = vpost
215 else
216 ! inside expansion fan
217 if (useisothermal) then ! this is a bit of a guess
218 dens(i) = rho_L*exp((xleftleft-xplot(i))/(cs_L*time) + v_L/cs_L)
219 else
220 dens(i) = rho_L*(gamfac*(xzero-xplot(i))/(cs_L*time) + gamfac*v_L/cs_L + (1.-gamfac))**(2./(gamma-1.))
221 endif
222 pr(i) = p_L*(dens(i)/rho_L)**gamma
223 vel(i) = (1.-gamfac)*(cs_L -(xzero-xplot(i))/time) + gamfac*v_L
224 endif
225 elseif (xplot(i) < xcontact) then
226 ! between left expansion fan/shock and contact discontinuity
227 ! post-shock, ahead of contact discontinuity but before right going wave
228 pr(i) = ppost
229 if (leftisshock) then
230 dens(i) = rho_L*(gamfac+ppost/p_L)/(1+gamfac*ppost/p_L)
231 else
232 dens(i) = rho_L*(ppost/p_L)**(1./gamma)
233 endif
234 vel(i) = vpost
235 elseif (xplot(i) < xright) then
236 ! post-shock, ahead of contact discontinuity but before right going wave
237 pr(i) = ppost
238 if (rightisshock) then
239 dens(i) = rho_R*(gamfac+ppost/p_R)/(1+gamfac*ppost/p_R)
240 else
241 dens(i) = rho_R*(ppost/p_R)**(1./gamma)
242 endif
243 vel(i) = vpost
244 elseif (xplot(i) < xrightright) then
245 if (rightisshock) then
246 ! irrelevant as in this case xrightright = xright
247 else
248 ! inside expansion fan to right
249 if (useisothermal) then ! this is a bit of a guess
250 dens(i) = rho_R*exp(-(xrightright-xplot(i))/(cs_R*time) - v_R/cs_R)
251 else
252 dens(i) = rho_R*(gamfac*(xplot(i)-xzero)/(cs_R*time) - gamfac*v_R/cs_R + (1.-gamfac))**(2./(gamma-1.))
253 endif
254 pr(i) = p_R*(dens(i)/rho_R)**gamma
255 vel(i) = (1.-gamfac)*(-cs_R - (xzero-xplot(i))/time) + gamfac*v_R
256 endif
257 else
258 ! undisturbed medium to the right
259 pr(i) = p_R
260 dens(i) = rho_R
261 vel(i) = v_R
262 endif
263 enddo
264
265 !--this is the beautiful, f95 version (which won't compile on pgf90)
266 ! where(xplot <= xleft) ! <= otherwise problems at t=0
267 !! undisturbed medium to the left
268 ! pr = p_L
269 ! dens = rho_L
270 ! vel = v_L
271 ! elsewhere(xplot < xfan)
272 !! inside expansion fan
273 ! dens = rho_L*(gamfac*(xzero-xplot)/(cs_L*time) + (1.-gamfac))**(2./(gamma-1.))
274 ! pr = p_L*(dens/rho_L)**gamma
275 ! vel = (1.-gamfac)*(cs_L -(xzero-xplot)/time)
276 ! elsewhere(xplot < xcontact)
277 !! between expansion fan and contact discontinuity
278 ! pr = ppost
279 ! dens = rho_L*(ppost/p_L)**(1./gamma)
280 ! vel = vpost
281 ! elsewhere(xplot < xshock)
282 !! post-shock, ahead of contact discontinuity
283 ! pr = ppost
284 ! dens = rho_R*(gamfac+ppost/p_R)/(1+gamfac*ppost/p_R)
285 ! vel = vpost
286 ! elsewhere
287 !! undisturbed medium to the right
288 ! pr = p_R
289 ! dens = rho_R
290 ! vel = v_R
291 ! end where
292
293 !------------------------------------
294 ! determine which solution to plot
295 !------------------------------------
296 select case(iplot)
297 case(1)
298 yplot = dens
299 case(2)
300 yplot = pr
301 case(3)
302 yplot = vel
303 case(4)
304 if (gamma > 1.0001) then
305 yplot = pr/((gamma-1.)*dens)
306 else
307 yplot = pr/dens
308 endif
309 case(5) ! deltav, where vd = 0
310 yplot = -vel
311 case(6) ! eps, where rhod = const
312 yplot = rho_R/(dens + rho_R)
313 end select
314
315 return
316 end subroutine exact_shock
317
318 !-------------------------------------------------------------------
319 ! Implementation of the exact Riemann solver given in Toro (1992)
320 !
321 ! Solves for the post-shock pressure (pr) and velocity (vstar)
322 ! given the initial left and right states
323 !
324 ! Does not matter if high P / high rho is on left or right
325 !
326 ! Daniel Price, Institute of Astronomy, Cambridge, UK, 2004
327 ! [email protected]
328 !-------------------------------------------------------------------
329 subroutine get_pstar(gamma,p_L,p_R,v_L,v_R,c_L,c_R,pr,vstar)
330 real, parameter :: tol = 1.5e-6
331 real, intent(in) :: gamma,p_L,p_R,v_L,v_R,c_L,c_R
332 real, intent(out) :: pr,vstar
333 integer, parameter :: maxits = 30
334 integer :: its
335 real :: prnew, f_L, f_R, dfdp_L, dfdp_R, f, df, dp
336 real :: power, denom
337 !
338 !--get an initial starting estimate of intermediate pressure
339 ! this one is from Toro(1992) - gives basically the right answer
340 ! for pressure jumps below about 4
341 !
342 power = (gamma-1.)/(2.*gamma)
343 denom = c_L/p_L**power + c_R/p_R**power
344 prnew = ((c_L + c_R + (v_L - v_R)*0.5*(gamma-1.))/denom)**(1./power)
345 pr = p_L
346 its = 0
347
348 !!print*,'initial guess = ',prnew
349
350 do while (abs(prnew-pr) > tol .and. its < maxits)
351
352 its = its + 1
353 pr = prnew
354 !
355 !--evaluate the function and its derivatives
356 !
357 call f_and_df(pr,p_L,c_L,gamma,f_L,dfdp_L)
358 call f_and_df(pr,p_R,c_R,gamma,f_R,dfdp_R)
359 !
360 !--then get new estimate of pr
361 !
362 f = f_L + f_R + (v_R - v_L)
363 df = dfdp_L + dfdp_R
364 !
365 !--Newton-Raphson iterations
366 !
367 dp = -f/df
368 prnew = pr + dp
369
370 enddo
371
372 if (its==maxits) print*,'WARNING: its not converged in riemann solver'
373 pr = prnew
374 vstar = v_L - f_L
375
376 print*,'its =',its,' p* =',prnew,'v* =',vstar,v_R + f_R
377
378 end subroutine get_pstar
379
380 !
381 !--pressure function
382 ! H is pstar/p_L or pstar/p_R
383 !
384 subroutine f_and_df(prstar,pr,cs,gam,fp,dfdp)
385 real, intent(in) :: prstar, pr, gam, cs
386 real, intent(out) :: fp,dfdp
387 real :: H,term, power, gamm1, denom
388
389 H = prstar/pr
390 gamm1 = gam - 1.
391
392 if (H > 1.) then ! shock
393 denom = gam*((gam+1.)*H + gamm1)
394 term = sqrt(2./denom)
395 fp = (H - 1.)*cs*term
396
397 dfdp = cs*term/pr + (H - 1.)*cs/term*(-1./denom**2)*gam*(gam+1.)/pr
398 else ! rarefaction
399 power = gamm1/(2.*gam)
400 fp = (H**power - 1.)*(2.*cs/gamm1)
401
402 dfdp = 2.*cs/gamm1*power*H**(power-1.)/pr
403 endif
404
405 end subroutine f_and_df
406
407 !-------------------------------------------------------------
408 ! Non-iterative isothermal Riemann solver
409 ! from Balsara (1994), ApJ 420, 197-212
410 !
411 ! See also Cha & Whitworth (2003), MNRAS 340, 73-90
412 !-------------------------------------------------------------
413 subroutine get_pstar_isothermal(cs2,v_L,v_R,rho_L,rho_R,pstar,vstar)
414 real, intent(in) :: cs2,v_L,v_R,rho_L,rho_R
415 real, intent(out) :: pstar,vstar
416 real :: sqrtrho_L, sqrtrho_R, X, vdiff, determinant, vstar2
417
418 sqrtrho_L = sqrt(rho_L)
419 sqrtrho_R = sqrt(rho_R)
420
421 X = sqrtrho_L*sqrtrho_R/(sqrtrho_L + sqrtrho_R)
422 vdiff = v_L - v_R
423 determinant = (X*vdiff)**2 + 4.*cs2*X*(sqrtrho_L + sqrtrho_R)
424
425 pstar = 0.25*(X*vdiff + sqrt(determinant))**2
426 vstar = v_L - (pstar - cs2*rho_L)/(sqrt(pstar*rho_L))
427 vstar2 = v_R + (pstar - cs2*rho_R)/(sqrt(pstar*rho_R))
428 print*,' pstar = ',pstar,' vstar = ',vstar,vstar2
429
430 end subroutine get_pstar_isothermal
431
432 end module shock