Debian Codegraph @

src/exact_toystar2D.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-2009 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !------------------------------------------------------------
24 ! Exact solutions for toystar in two dimensions
25 !
26 ! For details see Monaghan and Price (2005), in prep.
27 !
28 !------------------------------------------------------------
29 module toystar2D
30 implicit none
31 public :: exact_toystar2D
32 public :: etar, detadr ! public because they are used in setup
33
34 contains
35
36 !------------------------------------------------------------
37 ! calculate exact solution for toystar in two dimensions
38 !
39 ! non-linear solution solves ODEs, assumes linear velocity
40 !
41 ! the solutions are all plots against radius
42 !
43 ! iplot = 0 gives x vs y
44 !
45 ! iplot = 1->5 gives rho, pr, u, vx, vy vs r
46 !------------------------------------------------------------
47
48 subroutine exact_toystar2D(iplot,time,gamma,polyk,totmass, &
49 ampl,denscentre,C0,jorder,morder, &
50 V11,V22,V12,V21,xplot,yplot,ierr)
51 integer, intent(in) :: iplot,jorder,morder
52 integer, intent(out) :: ierr
53 real, intent(in) :: time,gamma,polyk,totmass
54 real, intent(in) :: C0, ampl, denscentre ! parameters for toy star
55 real, intent(in) :: V11,V22,V12,V21
56 real, dimension(:), intent(inout) :: xplot
57 real, dimension(:), intent(out) :: yplot
58
59 real, parameter :: pi = 3.141592653589
60 integer :: i,npts,nsteps
61 integer :: jmode,smode
62 real :: H,C,D,B,omega,omegasq
63 real :: radstar,dx,nu2 !!,scalefac
64 real :: rhoplot,deltarho,vplot,deltav
65 real :: gamm1,gam1,constK,sigma,period
66 real, dimension(8) :: params, paramsp,dparams
67 real :: fac,dt,t,phi,dphi,cosphi,sinphi,denom
68 real :: massbefore,massafter
69 logical linear
70
71 ierr = 1
72 npts = size(xplot)
73
74 linear = (jorder >= 0 .and. morder >= 0)
75 gamm1 = gamma - 1.
76 if (gamm1 < 1.e-3) then
77 print*,'Error: no toy star solution for isothermal yet'
78 ierr = 1
79 return
80 endif
81 gam1 = 1./gamm1
82 if (polyK <= 0.) then
83 print*,'Error: polytropic K <= 0 on input: using 0.25 by default'
84 constK = 0.25
85 else
86 constK = polyK !!0.25 ! this is K from P = K*rho**gamma
87 endif
88
89 omega = 1.0 ! this is omega from the main code (ie. from potential)
90 omegasq = omega**2
91 B = 0.
92 D = 0.
93
94 if (linear) then
95 !---------------------------------------------------------------------------
96 ! linear solution
97
98 print*,' Plotting 2D toy star: linear solution r mode = ',jorder,' phi mode = ',morder
99 jmode = jorder ! radial mode
100 smode = morder ! non-axisymmetric modes (theta)
101
102 ! sigma is the frequency of oscillation
103 nu2 = (jmode + smode)*(jmode+smode + 2./gamm1) - smode**2
104 if (nu2 <= 0.) then
105 print*,'Error: nu^2 < 0 in linear toy star ',nu2
106 print*,' radial mode = ',jmode,' theta mode = ',smode
107 ierr = 2
108 return
109 else
110 sigma = sqrt(0.5*omegasq*gamm1*nu2)
111 endif
112 print*,' Amplitude = ',ampl,' period = ',2*pi/sigma,' H,C = ',denscentre,C0
113
114 !!scalefac = polyk*gamma/(sigma*gamm1)
115 radstar = sqrt((2.*polyk*gamma*denscentre**gamm1)/gamm1)
116
117 xplot(1) = 0.
118 dx = (radstar-xplot(1))/float(npts-1)
119
120 do i=1,npts
121 xplot(i) = xplot(1)+dx*(i-1)
122 ! print*,i,' x,y = ',xplot(i),yplot(i)
123 rhoplot = (denscentre - C0*xplot(i)**2)
124 if (rhoplot <= 0.) rhoplot = 0.
125 deltarho = etar(jmode,smode,xplot(i)/radstar,gamma) ! functional form of rho(r)
126 !!print*,'deltarho = ',rhoplot,deltarho,xplot(i)
127 rhoplot = (rhoplot + deltarho*ampl*sin(sigma*time))**gam1
128
129 deltav = ampl*detadr(jmode,smode,xplot(i)/radstar,gamma)
130 vplot = deltav*cos(sigma*time)
131
132 select case(iplot)
133 case(1) ! plot solution for density
134 yplot(i) = rhoplot
135 case(2) ! plot solution for pressure
136 yplot(i) = constK*rhoplot**gamma
137 case(3) ! plot solution for utherm
138 yplot(i) = constK*(rhoplot**gamm1)/gamm1
139 case(4) ! plot solution for v_r
140 yplot(i) = vplot
141 case(5)
142 yplot(i) = vplot**2
143 end select
144
145 enddo
146
147 !---------------------------------------------------------------------------
148 ! non-linear solution
149 !
150 else
151
152 if (jorder < 0 .and. smode < 0) then
153 smode = 2
154 jmode = 0
155 else
156 smode = 0
157 jmode = 2
158 endif
159 print*,'Plotting 2D toy star: non-linear'
160 ! solve for H, C and A given initial conditions on v, rho and the time.
161 !
162 H = denscentre
163 !
164 !--this is the static solution, determined from the total mass, polyk, gamma and omega
165 !
166 radstar = sqrt(gamma*totmass/(pi*gamm1))
167 H = omegasq*gamm1*radstar**2/(2.*polyk*gamma)
168 C = 0.5*gamm1*omegasq/(gamma*polyk)
169 print*,' r_star = ',radstar,' rho = (',H,'-',C,'^2)**',gamm1
170 D = C
171 B = 0.
172 !
173 !--now solve the ODEs for V11,V22,V12,V21,H,C,D & B
174 ! (we use a simple modified euler method)
175 !
176 params(1)= V11
177 params(2)= V22
178 params(3)= V12
179 params(4)= V21
180 params(5)= H
181 params(6)= C
182 params(7)= D
183 params(8)= B
184 fac= 2.*gamma*polyk*gam1
185
186 massbefore = pi*gamm1/gamma*H**(gamma*gam1)/(sqrt(C*D - B**2))
187 !
188 !--get frequency to determine timestep
189 !
190 nu2 = (jmode + smode)*(jmode+smode + 2./gamm1) - smode**2
191 if (nu2 <= 0.) then
192 print*,'Error: nu^2 < 0 in exact toy star ',nu2
193 print*,' radial mode = ',jmode,' theta mode = ',smode
194 ierr = 2
195 return
196 else
197 sigma = sqrt(0.5*omegasq*gamm1*nu2)
198 endif
199 !
200 !--solve ODE's
201 !
202 period = 2.*pi/sigma
203 dt = 0.001*period
204 nsteps = int(time/dt)
205 dt = time/real(nsteps)
206 t = 0.
207 do i=1,nsteps
208 t = t + dt
209 call param_derivs(params,dparams,fac,gamm1,omegasq)
210 paramsp(:)= params(:) + 0.5*dt*dparams(:)
211 call param_derivs(paramsp,dparams,fac,gamm1,omegasq)
212 params(:)= params(:) + dt*dparams(:)
213 enddo
214 !
215 !--have now got solution at current time
216 !
217 H= params(5)
218 C= params(6)
219 D= params(7)
220 B= params(8)
221
222 print*,' solved ODEs to time = ',t,' in ',nsteps,' nsteps '
223 massafter = pi*gamm1/gamma*H**(gamma*gam1)/(sqrt(C*D - B**2))
224 print*,' conserved mass before = ',massbefore,' after =',massafter
225
226 if (C <= 0.) then
227 radstar = 0.5
228 stop '*** C = 0 = illegal'
229 !!elseif (A <= 1.e-5) then
230 else
231 radstar = sqrt(H/C)
232 endif
233 xplot(1) = 0.
234 dx = (radstar-xplot(1))/float(npts-1)
235
236 do i=1,npts
237 xplot(i) = xplot(1)+dx*(i-1)
238 ! print*,i,' x,y = ',xplot(i),yplot(i)
239 rhoplot = (H - C*xplot(i)**2)
240 if (rhoplot <= 0.) rhoplot = 0.
241 rhoplot = rhoplot**gam1
242 select case(iplot)
243 case(1) ! plot solution for density
244 yplot(i) = rhoplot
245 case(2) ! plot solution for pressure
246 yplot(i) = constK*rhoplot**gamma
247 case(3) ! plot solution for utherm
248 yplot(i) = constK*(rhoplot**gamm1)/gamm1
249 case(4) ! plot solution for v_r
250 yplot(i) = ampl*xplot(i)
251 case(5)
252 yplot(i) = (ampl*xplot(i))**2
253 end select
254 enddo
255 !
256 !------------------------------------------------------------------------
257 !
258 endif
259
260 if (iplot > 0 .and. iplot <= 5) then
261 ierr = 0
262 elseif (iplot==0) then
263 print*,' plotting non-axisymmetric boundary'
264 !
265 !--for x-y plots we plot the rho=0 curve (ie. toy star boundary)
266 !
267 dphi = 2.*pi/real(npts-1)
268 phi = 0.
269 do i=1,npts
270 phi = (i-1)*dphi
271 cosphi = cos(phi)
272 sinphi = sin(phi)
273 denom = C*cosphi**2 + 2.*B*cosphi*sinphi + D*sinphi**2
274 radstar = sqrt(H/denom)
275 xplot(i) = radstar*cosphi
276 yplot(i) = radstar*sinphi
277 enddo
278 ierr = 0
279 ! call pgsfs(2)
280 ! call pgcirc(0.0,0.0,radstar)
281 ! ierr = 3
282 endif
283
284 return
285 end subroutine exact_toystar2D
286
287 !
288 !--function that evaluates the polynomial for rho(r/re) for a given radial mode
289 ! (from the power series solution to the 2nd order ODE)
290 !
291 ! rad = r/r_star
292 ! j = radial (axisymmetric) mode
293 ! m = theta mode
294 !
295 ! solution is for delta(rho**(gamma-1))
296 ! ie. rho**(gamma-1) = rho_0**(gamma-1) + etar
297 !
298 ! and takes the form
299 !
300 ! etar = rad**m sum_k a_k rad**k
301 !
302 real function etar(j,m,rad,gamma)
303 integer, intent(in) :: j,m ! j is the radial mode, m is the theta mode
304 integer :: k,kprev
305 real, intent(in) :: rad,gamma
306 real :: denom,ak,akprev,gamm1,freqsq
307 !
308 !--this solution is for arbitrary gamma
309 !
310 gamm1 = gamma - 1.
311 if (gamm1 < 1.e-3) then
312 print*,'error gamma -1 <= 0'
313 etar = 0.
314 return
315 endif
316 !
317 !--the solution is of the form
318 ! drhor = a_0 + a_2 (r/re)**2 + a_4 (r/re)**4 + ...
319 ! where for j = k, coefficients >= a_k+2 are zero
320 !
321 freqsq = (j+m)*(j+m + 2./gamm1) - m**2
322
323 akprev = 1.0 ! this is a_0 which is the amplitude
324 etar = akprev
325 !!print*,'mode = ',j,m,' nu^2 = ',freqsq,' a_0 = ',akprev
326 !
327 !--the co-efficients for the terms above a_0 are calculated using
328 ! the recurrence relation between the a_k's
329 !
330 do k = 2,j,2
331 kprev = k-2
332 denom = real((kprev + 2 + m)**2 - m**2)
333 ak = akprev*(kprev**2 + 2.*kprev*m + 2.*(kprev+m)/gamm1 - freqsq)/denom
334 !!print*,'coeff ',k,' = ',ak,k**2,2.*k/gamm1
335 etar = etar + ak*rad**k
336 akprev = ak
337 enddo
338
339 etar = etar * rad**m
340
341 end function etar
342
343 !
344 !--function that evaluates the polynomial for v(r/re) for a given radial mode
345 ! (from the power series solution to the 2nd order ODE)
346 !
347 real function detadr(j,m,rad,gamma)
348 integer, intent(in) :: j, m ! j is the radial mode, m is the theta mode
349 integer :: k,kprev
350 real, intent(in) :: rad,gamma
351 real :: denom,term1,term2
352 real :: ak,akprev,gamm1,freqsq
353 !
354 !--this solution is for arbitrary gamma
355 !
356 gamm1 = gamma - 1.
357 if (gamm1 < 1.e-3) then
358 print*,'error gamma -1 <= 0'
359 detadr = 0.
360 return
361 endif
362 !
363 !--the solution is of the form
364 ! drhor = a_0 + a_2 (r/re)**2 + a_4 (r/re)**4 + ...
365 ! where for j = k, coefficients >= a_k+2 are zero
366 !
367 freqsq = (j+m)*(j+m + 2./gamm1) - m**2
368
369 detadr = 0.
370 akprev = 1.0 ! this is a_0 which is the amplitude
371 term1 = akprev
372 term2 = 0.
373 ! print*,'mode = ',j,m,' nu^2 = ',freqsq,' a_0 = ',akprev
374 !
375 !--the co-efficients for the terms above a_0 are calculated using
376 ! the recurrence relation between the a_k's
377 !
378 do k = 2,j,2
379 kprev = k-2
380 denom = real((kprev + 2 + m)**2 - m**2)
381 ak = akprev*(kprev**2 + 2.*kprev*m + 2.*(kprev+m)/gamm1 - freqsq)/denom
382 !!print*,'coeff ',k,' = ',ak,k*ak,rad,(k-1)
383 term1 = term1 + ak*rad**k
384 term2 = term2 + k*ak*rad**(k-1)
385 akprev = ak
386 enddo
387
388 if (m==0) then
389 detadr = term2
390 else
391 detadr = m*rad**(m-1)*term1 + rad**m*term2
392 endif
393
394 end function detadr
395
396 subroutine param_derivs(func,dfunc,fac,gamm1,omegasq)
397 real, intent(in), dimension(8) :: func
398 real, intent(out), dimension(8) :: dfunc
399 real, intent(in) :: fac, gamm1,omegasq
400 real :: term, gamma
401
402 term = func(1) + func(2)
403 gamma = gamm1 + 1.
404 dfunc(1)= fac*func(6) - func(1)*func(1) - func(3)*func(4) - omegasq
405 dfunc(2)= fac*func(7) - func(2)*func(2) - func(3)*func(4) - omegasq
406 dfunc(3)= fac*func(8) - func(3)*term
407 dfunc(4)= fac*func(8) - func(4)*term
408 dfunc(5)= -gamm1*term*func(5)
409 dfunc(6)= -2.*func(6)*func(1) - gamm1*func(6)*term - 2.*func(8)*func(4)
410 dfunc(7)= -2.*func(7)*func(2) - gamm1*func(7)*term - 2.*func(8)*func(3)
411 dfunc(8)= -func(6)*func(3) - func(7)*func(4) - gamma*func(8)*term
412
413 return
414 end subroutine param_derivs
415
416 !----------------------------------------------------------------------
417 !
418 ! this subroutine plots the alpha-beta relation in the 2D Toy star solution
419 !
420 !----------------------------------------------------------------------
421 subroutine exact_toystar_ACplane2D(astart,bstart,sigmain,gamma)
422 use plotlib, only:plot_swin,plot_box,plot_label,plot_line
423 real, intent(in) :: astart,bstart,sigmain,gamma
424 integer, parameter :: npts = 2000
425 integer :: i
426 real :: gamm1,gam1,sigma
427 real :: polyk,Omega2,constk
428 real :: xstart,xend,ymin,ymax,xi,term,extra
429 real, dimension(npts) :: xplot, yplot
430
431 print*,' plotting alpha-beta plane...'
432
433 gamm1 = gamma - 1.
434 gam1 = 1./gamm1
435 polyk = 0.25
436 Omega2 = 1.0
437 sigma = 1.0
438 print*,' alpha = ',astart,' beta = ',bstart, 'sigma = ',sigma,sigmain
439 !
440 !--find integration constant from starting values of alpha and beta
441 !
442 constk = (astart**2 + bstart**2 + Omega2 &
443 + 2.*polyk*gamma*(sigma*bstart**gamma)*gam1**2)/bstart
444
445 print*,' integration constant = ',constk
446
447 !
448 !--find limits of plot (ie. where alpha = 0)
449 !
450 xstart = 0.25
451 xend = 2.0
452
453 ! print*,'plotting k = ',k,' cstart = ',cstart,' astart = ',astart
454 ! print*,'min c = ',xstart,' max c = ',xend
455
456 xstart = xstart + 0.000001
457 xend = xend - 0.000001
458
459 extra = 0.1*(xend-xstart)
460 !!xcentre = 0.5*(xstart + xend)
461 ymax = 2.0
462 ymin = -2.0
463
464 call plot_swin(xstart-extra,xend+extra,ymin,ymax)
465 call plot_box('bcnst',0.0,0,'1bvcnst',0.0,0)
466 call plot_label ('beta','alpha',' ')
467
468 do i=1,npts
469 xi = xstart + (i-1)*npts
470 xplot(i) = xi
471 term = -(xi**2 + Omega2 + 2.*polyk*gamma*(sigma*xi**gamma)*gam1**2 + constk*xi)
472 if (term <= 0) then
473 yplot(i) = 0.
474 else
475 yplot(i) = sqrt(term)
476 endif
477 enddo
478
479 call plot_line(npts,xplot,yplot)
480 return
481
482 end subroutine exact_toystar_ACplane2D
483 end module toystar2D