solution.py 6.49 KB
Newer Older
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
"""
Peter Creasey
p.e.creasey.00@googlemail.com

solution to the Sedov problem

based on the C code by Aamer Haque
"""
from scipy.special import gamma as Gamma
from numpy import power, arange, empty, float64, log, exp, pi, diff, inner, outer, array


def calc_a(g,nu=3):
    """ 
    exponents of the polynomials of the sedov solution
    g - the polytropic gamma
    nu - the dimension
    """
    a = [0]*8
   
    a[0] = 2.0 / (nu + 2)
    a[2] = (1-g) / (2*(g-1) + nu)
    a[3] = nu / (2*(g-1) + nu)
    a[5] = 2 / (g-2)
    a[6] = g / (2*(g-1) + nu)
   
    a[1] = (((nu+2)*g)/(2.0+nu*(g-1.0)) ) * ( (2.0*nu*(2.0-g))/(g*(nu+2.0)**2) - a[2])
    a[4] = a[1]*(nu+2) / (2-g)
    a[7] = (2 + nu*(g-1))*a[1]/(nu*(2-g))
    return a

def calc_beta(v, g, nu=3):
    """ 
    beta values for the sedov solution (coefficients of the polynomials of the similarity variables) 
    v - the similarity variable
    g - the polytropic gamma
    nu- the dimension
    """

    beta = (nu+2) * (g+1) * array((0.25, (g/(g-1))*0.5,
            -(2 + nu*(g-1))/2.0 / ((nu+2)*(g+1) -2*(2 + nu*(g-1))),
     -0.5/(g-1)), dtype=float64)

    beta = outer(beta, v)

    beta += (g+1) * array((0.0,  -1.0/(g-1),
                           (nu+2) / ((nu+2)*(g+1) -2.0*(2 + nu*(g-1))),
                           1.0/(g-1)), dtype=float64).reshape((4,1))

    return beta


def sedov(t, E0, rho0, g, n=1000, nu=3):
    """ 
    solve the sedov problem
    t - the time
    E0 - the initial energy
    rho0 - the initial density
    n - number of points (10000)
    nu - the dimension
    g - the polytropic gas gamma
    """
    # the similarity variable
    v_min = 2.0 / ((nu + 2) * g)
    v_max = 4.0 / ((nu + 2) * (g + 1))

    v = v_min + arange(n) * (v_max - v_min) / (n - 1.0)

    a = calc_a(g, nu)
    beta = calc_beta(v, g=g, nu=nu)
    lbeta = log(beta)
    
    r = exp(-a[0] * lbeta[0] - a[2] * lbeta[1] - a[1] * lbeta[2])
    rho = ((g + 1.0) / (g - 1.0)) * exp(a[3] * lbeta[1] + a[5] * lbeta[3] + a[4] * lbeta[2])
    p = exp(nu * a[0] * lbeta[0] + (a[5] + 1) * lbeta[3] + (a[4] - 2 * a[1]) * lbeta[2])
    u = beta[0] * r * 4.0 / ((g + 1) * (nu + 2))
    p *= 8.0 / ((g + 1) * (nu + 2) * (nu + 2))

    # we have to take extra care at v=v_min, since this can be a special point.
    # It is not a singularity, however, the gradients of our variables (wrt v) are.
    # r -> 0, u -> 0, rho -> 0, p-> constant

    u[0] = 0.0; rho[0] = 0.0; r[0] = 0.0; p[0] = p[1]

    # volume of an n-sphere
    vol = (pi ** (nu / 2.0) / Gamma(nu / 2.0 + 1)) * power(r, nu)

    # note we choose to evaluate the integral in this way because the
    # volumes of the first few elements (i.e near v=vmin) are shrinking 
    # very slowly, so we dramatically improve the error convergence by 
    # finding the volumes exactly. This is most important for the
    # pressure integral, as this is on the order of the volume.

    # (dimensionless) energy of the model solution
    de = rho * u * u * 0.5 + p / (g - 1)
    # integrate (trapezium rule)
    q = inner(de[1:] + de[:-1], diff(vol)) * 0.5

    # the factor to convert to this particular problem
    fac = (q * (t ** nu) * rho0 / E0) ** (-1.0 / (nu + 2))

    # shock speed
    shock_speed = fac * (2.0 / (nu + 2))
    rho_s = ((g + 1) / (g - 1)) * rho0                                                                            
    r_s = shock_speed * t * (nu + 2) / 2.0
    p_s = (2.0 * rho0 * shock_speed * shock_speed) / (g + 1)
    u_s = (2.0 * shock_speed) / (g + 1)
    
    r *= fac * t
    u *= fac
    p *= fac * fac * rho0
    rho *= rho0
    return r, p, rho, u, r_s, p_s, rho_s, u_s, shock_speed


def test():
    """ draw a 3d sedov solution """
    import pylab as pl
    gamma = 5.0/3.0
    r,p,rho,u,r_s,p_s,rho_s,u_s,shock_speed = \
        sedov(t=0.05, E0=5.0, rho0=5.0, g=gamma)

    print 'rho shock', rho_s
    print 'p shock', p_s
    print 'u shock', u_s
    print 'r shock', r_s

    print 'Dimensionless var (E/rho) t^2 r^-5', (5.0 /5.0)* 0.05**0.4 * r[-1]**-1.0
    vols = (4/3.0)*pi*r*r*r
    dv = vols.copy()
    dv[1:] = diff(dv)

    # thermal and kinetic energy
    te = (p*dv/(gamma-1))
    ke = (rho*u*u*0.5*dv)
    energy = te.sum() + ke.sum()
    mass = 0.5*inner(rho[1:]+rho[:-1],dv[1:])

    print 'density', mass / (4/3.0 * pi * r_s**3)
    print 'energy', energy
    print 'shock speed', shock_speed
    pl.plot(r/r_s,rho/rho_s, label=r'$\rho/\rho_s$')
    pl.plot(r/r_s,p/p_s, label=r'$p/p_s$')
    pl.plot(r/r_s,u/u_s, label=r'$u/u_s$')
    pl.legend(loc='upper left')
    pl.show()

def test2():
    """ test momentum and mass conservation in 3d """
    import pylab as pl
    r,p,rho,u,r_s,p_s,rho_s,u_s,shock_speed = \
        sedov(t=0.05, E0=5.0, rho0=5.0, g=5.0/3.0,n=10000)

    dt = 1e-5
    r2,p2,rho2,u2 = sedov(t=0.05+dt, E0=5.0, rho0=5.0, g=5.0/3.0, n=9000)[:4]

    # align the results
    from numpy import interp, gradient
    p2 = interp(r,r2,p2)
    rho2 = interp(r,r2,rho2)
    u2 = interp(r,r2,u2)

    # mass conservation
    pl.plot(r, -gradient(rho*u*r*r)/(r*r*gradient(r)), 'b', label=r'$\frac{1}{r^2}\frac{\partial}{\partial r} \rho u r^2$')
    pl.plot(r, (rho2-rho)/dt, 'k', label=r'$\frac{\partial \rho}{\partial t}$')

    # momentum conservation
    pl.plot(r, -gradient(p)/gradient(r), 'g',label=r'$-\frac{\partial p}{\partial r}$')
    pl.plot(r, rho*((u2-u)/dt+u*gradient(u)/gradient(r)), 'r',label=r'$\rho \left( \frac{\partial u}{\partial t} + u\frac{\partial u}{\partial r} \right)$')

    pl.legend(loc='lower left')
    pl.show()        

def test3():
    """ draw a 2d sedov solution """
    import pylab as pl
    r,p,rho,u,r_s,p_s,rho_s,u_s,shock_speed = sedov(t=1.2, E0=1, rho0=1, g=5.0/3.0, nu=2)

    print 'rho shock', rho_s
    print 'p shock', p_s
    print 'u shock', u_s
    print 'r shock', r_s
  
    area = pi*r*r
    dv = area.copy()
    dv[1:] = diff(dv)

    # thermal and kinetic energy
    te = (p*dv/(5.0/3.0-1))
    ke = (rho*u*u*0.5*dv)
    #pl.plot(arange(te.size), ke, 'x')
    #pl.show()
    print 'r0', r[:2]
    energy = te.sum() + ke.sum()
    mass = 0.5*inner(rho[1:]+rho[:-1],dv[1:])

    print 'density', mass / (pi * r_s**2)
    print 'energy', energy
    print 'shock speed', shock_speed
    #pl.plot(r/r_s,rho/rho_s, 'b,',label=r'$\rho/\rho_s$')
    #pl.plot(r/r_s,p/p_s,'r',label=r'$p/p_s$')
    #pl.plot(r/r_s,u/u_s, 'g,',label=r'$u/u_s$')
#    pl.plot(r,rho,'b',label='$\\rho$')
    pl.plot(r,u,'b',label='$u$')
#    pl.plot(r,p,'b',label='$P$')
    pl.legend(loc='upper left')
#    pl.xlim(0,2)
#    pl.ylim(0,5)
    pl.show()

if __name__=='__main__':
212
    test()
213
    #test2()
214
    # test3()