Debian Codegraph @

src/exact_toystar1D.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 ! plot exact solution for toystar in one dimension
25 !
26 ! linear solution uses Gegenbauer/Legendre polynomials
27 !
28 ! non-linear solution solves ODEs, assumes linear velocity
29 !
30 ! For details see Monaghan and Price (2004) MNRAS
31 !------------------------------------------------------------
32 module toystar1D
33 implicit none
34 public :: exact_toystar1D, exact_toystar_ACplane
35 private :: Pm, Gn
36
37 contains
38
39 subroutine exact_toystar1D(iplot,time,gamma,H0,A0,C0, &
40 sigma,norder,xplot,yplot,npts,ierr)
41 use plotlib, only:plot_pt1
42 integer, intent(in) :: iplot,norder,npts
43 integer, intent(out) :: ierr
44 real, intent(in) :: time,gamma,sigma
45 real, intent(in) :: H0, C0, A0 ! parameters for toy star
46 real, dimension(:), intent(inout) :: xplot
47 real, dimension(size(xplot)), intent(out) :: yplot
48
49 integer :: i,nsteps
50 real :: const ! parameter for toy star
51 real :: Hprev, Cprev, Aprev, Htemp, Ctemp, Atemp, H, C, A
52 real :: radstar,dx,dt,tnow
53 real :: rhoplot,deltarho
54 real :: fprevC,fprevA,fprevH,ftempC,ftempA,ftempH
55 real :: gamp1,gamm1,gam1,fact,constK,omega
56 real :: fnorm
57 logical :: linear
58
59 linear = .false.
60 if (norder >= 0) linear = .true.
61 gamp1 = gamma + 1.
62 gamm1 = gamma - 1.
63 gam1 = 1./gamm1
64 constK = 0.25
65
66 ierr = 0
67
68 if (linear) then
69 !---------------------------------------------------------------------------
70 ! linear solution uses Gegenbauer & Legendre Polynomials
71 ! (this is for the toy star oscillations)
72
73 omega = sqrt(0.5*(norder+1.)*(norder+2.))
74 fnorm = 2.*(norder+1)*(norder+2)/real(2.*norder + 3.)
75
76 print*,' Plotting toy star oscills: time, norder, omega = ', &
77 time,norder,omega,H0,C0,A0
78
79 if (C0 <= 0.) then
80 print*,'*** C = 0 = illegal in input'
81 ierr = 1
82 return
83 else
84 radstar = sqrt(H0/C0)
85 endif
86 xplot(1) = -radstar
87 dx = (radstar-xplot(1))/float(npts-1)
88
89 do i=2,npts
90 xplot(i) = xplot(1)+dx*(i-1)
91 ! print*,i,' x,y = ',xplot(i),yplot(i)
92 rhoplot = (H0 - C0*xplot(i)**2)
93 if (rhoplot <= 0.) rhoplot = 0.
94 deltarho = Pm(xplot(i),norder+1)*sin(omega*time)
95 rhoplot = rhoplot**gam1 + 2.*omega*A0*deltarho/fnorm
96 select case(iplot)
97 case(1) ! plot solution for density
98 yplot(i) = rhoplot
99 case(2) ! plot solution for pressure
100 yplot(i) = constK*rhoplot**gamma
101 case(3) ! plot solution for utherm
102 yplot(i) = constK*(rhoplot**gamm1)/gamm1
103 case(4) ! plot solution for vx
104 yplot(i) = A0*Gn(xplot(i),norder)*cos(omega*time)
105 case(5) ! plot solution for By
106 yplot(i) = sigma*rhoplot
107 end select
108
109 enddo
110
111 if (iplot==6) then ! plot By \propto rho
112 dx = (H0**gam1)/float(npts-1) ! ie (rhomax - 0)/npts
113 xplot(1) = 0.
114 yplot(1) = sigma*xplot(1)
115 do i=2,npts
116 xplot(i) = xplot(1) + dx*(i-1)
117 yplot(i) = sigma*(xplot(i))
118 enddo
119 endif
120
121 if (iplot==7) then ! plot current point on A-C plane
122 call plot_pt1(C0,A0*cos(omega*time),4)
123 ierr = 2 ! do not plot again
124 else ! plot normal exact solution line
125 ierr = 0
126 endif
127 !---------------------------------------------------------------------------
128 ! non-linear solution for the fundamental (n=1) mode
129 !
130 else
131 !
132 ! solve for H, C and A given initial conditions on v, rho and the time.
133 !
134 nsteps = 1000*(int(time) + 1)
135
136 Hprev = H0
137 Cprev = C0
138 Aprev = A0
139
140 ! PRINT*,' nsteps,H,C,A in = ',nsteps,H0,C0,A0
141 dt = time/nsteps
142
143 fact = 2.*(constK + 0.5*sigma**2)*gamma*gam1
144
145 const = (A0**2 + 1. + 2.*fact*C0*gam1)*C0**(-2./gamp1)
146
147 print*,' Plotting toy star: time, H0, C0, A0, k = ', &
148 time,Hprev,Cprev,Aprev,const
149
150 tnow = 0.
151 do i = 1,nsteps
152 tnow = tnow + dt
153 ! integrate using improved Euler
154 fprevC = -Cprev*Aprev*gamp1
155 fprevA = fact*Cprev -1.-Aprev**2
156 fprevH = -Aprev*Hprev*gamm1
157 ! predictor
158 Ctemp = Cprev + dt*(-Cprev*Aprev*gamp1)
159 Atemp = Aprev + dt*(fact*Cprev-1.-Aprev**2)
160 Htemp = Hprev + dt*(-Aprev*Hprev*gamm1)
161
162 ftempC = -Ctemp*Atemp*gamp1
163 ftempA = fact*Ctemp -1. -Aprev**2
164 ftempH = -Atemp*Htemp*gamm1
165 ! corrector
166 C = Cprev + 0.5*dt*(fprevC + ftempC)
167 A = Aprev + 0.5*dt*(fprevA + ftempA)
168 H = Hprev + 0.5*dt*(fprevH + ftempH)
169
170 Cprev = C
171 Aprev = A
172 Hprev = H
173
174 ! print*,' time = ',tnow
175 ! if ((abs(C-C0) < 5.e-3).AND. &
176 ! (abs(A-A0) < 5.e-3).AND.(tnow > 5e-3)) then
177 ! PRINT*,'*** period, t = ',tnow,' err = ',abs(C-C0)+abs(A-A0)
178 ! ENDIF
179
180 enddo
181
182 const = (A**2 + 1. + 2.*fact*C*gam1)*C**(-2./gamp1)
183
184 print*,' C, A, H, k = ',C,A,H,const
185
186 if (C <= 0.) then
187 radstar = 0.5
188 print*,'*** C = 0 = illegal'
189 ierr = 1
190 return
191 else
192 radstar = sqrt(H/C)
193 endif
194 xplot(1) = -radstar
195 dx = (radstar-xplot(1))/float(npts-1)
196
197 do i=2,npts
198 xplot(i) = xplot(1)+dx*(i-1)
199 ! print*,i,' x,y = ',xplot(i),yplot(i)
200 rhoplot = (H - C*xplot(i)**2)
201 if (rhoplot <= 0.) rhoplot = 0.
202 rhoplot = rhoplot**gam1
203 select case(iplot)
204 case(1) ! plot solution for density
205 yplot(i) = rhoplot
206 case(2) ! plot solution for pressure
207 yplot(i) = constK*rhoplot**gamma
208 case(3) ! plot solution for utherm
209 yplot(i) = constK*(rhoplot**gamm1)/gamm1
210 case(4) ! plot solution for vx
211 yplot(i) = A*xplot(i)
212 case(5) ! plot solution for By
213 yplot(i) = sigma*rhoplot
214 end select
215
216 enddo
217
218 if (iplot==6) then ! plot By \propto rho
219 dx = (H**gam1)/float(npts-1) ! ie (rhomax - 0)/npts
220 xplot(1) = 0.
221 yplot(1) = sigma*xplot(1)
222 do i=2,npts
223 xplot(i) = xplot(1) + dx*(i-1)
224 yplot(i) = sigma*(xplot(i))
225 enddo
226 endif
227
228 if (iplot==7) then ! plot current point on A-C plane
229 call plot_pt1(C,A,4)
230 ierr = 2 ! do not plot again
231 else ! plot normal exact solution line
232 ierr = 0
233 endif
234 !
235 !------------------------------------------------------------------------
236 !
237 endif
238
239 return
240 end subroutine exact_toystar1D
241 !
242 !--function to evaluate the Gegenbauer polynomial of index n given x
243 !
244 real function Gn(x,n)
245 integer, intent(in) :: n
246 real, intent(in) :: x
247 integer :: i
248 real :: Gnminus1,Gnminus2
249 real :: fnorm
250
251 fnorm = 2.*(n+1)*(n+2)/real(2.*n + 3.)
252 ! PRINT*,' fnorm = ',fnorm
253 !
254 !--specify first two Gegenbauer polynomials
255 !
256 Gnminus2 = 1.
257 Gnminus1 = 3.*x
258 Gn = 0. ! avoid compiler warning
259 !
260 !--use recurrence relation to calculate the rest
261 !
262 select case (n)
263 case (0)
264 Gn = Gnminus2
265 case (1)
266 Gn = Gnminus1
267 case (2:)
268 do i=2,n
269 Gn = ((2*i+1)*x*Gnminus1 - (i+1)*Gnminus2)/real(i)
270 Gnminus2 = Gnminus1
271 Gnminus1 = Gn
272 enddo
273 end select
274
275 Gn = Gn/fnorm
276
277 end function Gn
278
279 !
280 !--function to calculate a Legendre Polynomial of order m
281 !
282 real function Pm(x,m)
283 integer, intent(in) :: m
284 real, intent(in) :: x
285 integer :: i
286 real :: Pmminus1,Pmminus2
287 !
288 !--specify first two Legendre polynomials
289 !
290 Pmminus2 = 1.
291 Pmminus1 = x
292 Pm = 0. ! avoid compiler warning
293
294 select case(m)
295 case (0)
296 Pm = 1.
297 case (1)
298 Pm = x
299 case (2:) ! use recurrence relation to calculate the rest
300 do i=2,m
301 Pm = ((2.*(i-1.)+1.)*x*Pmminus1 - (i-1.)*Pmminus2)/real(i)
302 Pmminus2 = Pmminus1
303 Pmminus1 = Pm
304 enddo
305 end select
306
307 end function Pm
308
309
310 !----------------------------------------------------------------------
311 !
312 ! this subroutine plots the A-C relation in the 1D Toy star solution
313 !
314 !----------------------------------------------------------------------
315 subroutine exact_toystar_ACplane(astart,cstart,sigma,gamma)
316 use plotlib, only:plot_swin,plot_funx,plot_label,plot_box
317 real, intent(in) :: astart,cstart,sigma,gamma
318 real :: constk,gam1,gamm1,gamp1,fact
319 real :: xstart,xend,xcentre,c,cnew,k
320 real :: func,func2,funct,fderiv,ymin,ymax,extra
321 external func,func2
322 common /kconst/ k,fact,gam1,gamp1
323
324 print*,' plotting a-c plane...'
325
326 gamp1 = gamma + 1.
327 gamm1 = gamma - 1.
328 gam1 = 1./gamm1
329 constk = 0.25
330 ! print*,' k, kdash = ',constk,constk + 0.5*sigma**2
331 fact = 2.*(constk + 0.5*sigma**2)*gamma*gam1
332
333 k = (astart**2 + 1. + 2.*fact*cstart*gam1)*cstart**(-2./gamp1)
334
335 ! print*,' k,fact = ',k,fact
336 !
337 !--find limits of plot (ie. where a = 0)
338 !
339 c = 1.e6
340 cnew = 0.25
341
342 do while (abs(c-cnew) > 1.e-5)
343
344 c = cnew
345
346 funct = k*c**(2./gamp1) - 2.*fact*c*gam1 - 1.
347 fderiv = 2.*k/gamp1*c**(-gamm1/gamp1) - 2.*fact*gam1
348
349 cnew = c - funct/fderiv
350
351 if (cnew < 0.) print*,'eek c < 0'
352
353 enddo
354
355 xstart = cnew
356
357 c = 1.e6
358 cnew = 6.37935
359
360 do while (abs(c-cnew) > 1.e-5)
361
362 c = cnew
363
364 funct = k*c**(2./gamp1) - 2.*fact*c*gam1 - 1.
365 fderiv = 2.*k/gamp1*c**(-gamm1/gamp1) - 2.*fact*gam1
366
367 cnew = c - funct/fderiv
368
369 enddo
370
371 xend = cnew
372
373 ! print*,'plotting k = ',k,' cstart = ',cstart,' astart = ',astart
374 ! print*,'min c = ',xstart,' max c = ',xend
375
376 xstart = xstart + 0.000001
377 xend = xend - 0.000001
378
379 extra = 0.1*(xend-xstart)
380 xcentre = 0.5*(xstart + xend)
381 ymax = 1.5*func(xcentre)
382 ymin = 1.5*func2(xcentre)
383 call plot_swin(xstart-extra,xend+extra,ymin,ymax)
384 call plot_box('bcnst',0.0,0,'1bvcnst',0.0,0)
385 call plot_funx(func,10000,xstart,xend,1)
386 call plot_funx(func2,10000,xstart,xend,1)
387
388 call plot_label ('c','a',' ')
389 return
390
391 end subroutine exact_toystar_ACplane
392
393 end module toystar1D
394
395 !------------------------------------
396 !
397 ! these functions must be external
398 !
399 !------------------------------------
400 real function func(x)
401 real, intent(in) :: x
402 real :: k,term,fact,gam1,gamp1
403 common /kconst/ k,fact,gam1,gamp1
404
405 ! print*,'k = ',k
406
407 term = -1 -2.*fact*x*gam1 + k*x**(2./gamp1)
408 if (term <= 0.) then
409 ! print*,' warning: func < 0 ',term
410 func = 0.
411 else
412 func = sqrt(term)
413 endif
414
415 end function func
416
417 real function func2(x)
418 implicit none
419 real, intent(in) :: x
420 real :: k,term,fact,gam1,gamp1
421 common /kconst/ k,fact,gam1,gamp1
422
423 ! print*,' k = ',k
424
425 term = -1 -2.*fact*x*gam1 + k*x**(2./gamp1)
426 if (term <= 0.) then
427 func2 = 0.
428 else
429 func2 = -sqrt(term)
430 endif
431
432 end function func2