kernels.py 5.46 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
#!/usr/bin/env python2                                  
# -*- coding: utf-8 -*-            
import matplotlib
matplotlib.use("Agg")
from pylab import *
from scipy import integrate
import distinct_colours as colours
from scipy.optimize import curve_fit
from scipy.optimize import fsolve
from matplotlib.font_manager import FontProperties
import numpy

params = {
    'axes.labelsize': 8,
    'axes.titlesize': 8,
    'font.size': 8,
    'legend.fontsize': 9,
    'xtick.labelsize': 8,
    'ytick.labelsize': 8,
    'xtick.major.pad': 2.5,
    'ytick.major.pad': 2.5,
    'text.usetex': True,
'figure.figsize' : (3.15,3.15),
'figure.subplot.left'    : 0.12,
'figure.subplot.right'   : 0.99  ,
'figure.subplot.bottom'  : 0.08  ,
'figure.subplot.top'     : 0.99  ,
'figure.subplot.wspace'  : 0.  ,
'figure.subplot.hspace'  : 0.  ,
'lines.markersize' : 6,
'lines.linewidth' : 2.,
'text.latex.unicode': True
}
rcParams.update(params)
rc('font',**{'family':'sans-serif','sans-serif':['Times']})


#Parameters
eta = 1.2349
h = 2.1

#Constants
PI = math.pi

#Cubic Spline
cubic_kernel_degree = 3
cubic_kernel_ivals = 2
cubic_kernel_gamma = 2.
cubic_kernel_ngb = 4.0 / 3.0 * PI * eta**3 * 6.0858
cubic_kernel_coeffs = array([[3./(4.*PI) , -3./(2.*PI), 0.,     1./PI],
                             [-1./(4.*PI),  3./(2.*PI), -3./PI, 2./PI],
                             [0.,           0.,         0.,     0.]])
def cubic_W(x):
    if size(x) == 1:
        x = array([x])
    ind = (minimum(x, cubic_kernel_ivals)).astype(int)
    coeffs = cubic_kernel_coeffs[ind,:]
    w = coeffs[:,0] * x + coeffs[:,1]
    for k in range(2, cubic_kernel_degree+1):
        w = x * w + coeffs[:,k]
    return w


#Quartic Spline
quartic_kernel_degree = 4
quartic_kernel_ivals = 3
quartic_kernel_gamma = 2.5
quartic_kernel_ngb = 4.0 / 3.0 * PI * eta**3 * 8.2293
quartic_kernel_coeffs = array([[3./(10.*PI) , 0.,           -3./(4.*PI) , 0.          ,  23./(32.*PI)],
                               [-1./(5.*PI) , 1./PI       , -3./(2.*PI) ,1./(4.*PI)   ,  11./(16.*PI)],
                               [1./(20.*PI) , -1./(2.*PI) , 15./(8.*PI) , -25./(8.*PI), 125./(64.*PI)],
                               [ 0. , 0.,           0.,         0.,     0.]])
def quartic_W(x):
    if size(x) == 1:
        x = array([x])
    ind = (minimum(x+0.5, quartic_kernel_ivals)).astype(int)
    coeffs = quartic_kernel_coeffs[ind,:]
    w = coeffs[:,0] * x + coeffs[:,1]
    for k in range(2, quartic_kernel_degree+1):
        w = x * w + coeffs[:,k]
    return w


# Wendland kernel
wendland2_kernel_degree = 5
wendland2_kernel_ivals = 1
wendland2_kernel_gamma = 2
wendland2_kernel_ngb = 4.0 / 3.0 * PI * eta**3 * 7.261825
wendland2_kernel_coeffs = 3.342253804929802 * array([[1./8, -30./32, 80./32, -80./32., 0., 1.],
                                                     [ 0. , 0.,  0.,   0., 0., 0.]]) / 8.

print wendland2_kernel_coeffs
def wendland2_W(x):
    if size(x) == 1:
        x = array([x])
    ind = (minimum(0.5*x, wendland2_kernel_ivals)).astype(int)
    coeffs = wendland2_kernel_coeffs[ind,:]
    w = coeffs[:,0] * x + coeffs[:,1]
    for k in range(2, wendland2_kernel_degree+1):
        w = x * w + coeffs[:,k]
    return w 

#def wendland2_W(x):
#    if size(x) == 1:
#        x = array([x])
#    x /= 1.936492
#    x[x>1.] = 1.
#    oneminusu = 1.-x
#    oneminusu4 = oneminusu * oneminusu * oneminusu * oneminusu
#    return 3.342253804929802 * oneminusu4 * (1. + 4.*x) / h**3


#Find H
r = arange(0, 3.5*h, 1./1000.)
xi = r/h
cubic_Ws = cubic_W(xi)
quartic_Ws = quartic_W(xi)
wendland2_Ws = wendland2_W(xi)
for j in range(size(r)):
    if cubic_Ws[j] == 0:
        cubic_H = r[j]
        break
for j in range(size(r)):
    if quartic_Ws[j] == 0:
        quartic_H = r[j]
        break
for j in range(size(r)):
    if wendland2_Ws[j] == 0:
        wendland2_H = r[j]
        break

    
print "H=", cubic_H
print "H=", quartic_H
print "H=", wendland2_H


# Compute sigma -----------------------------------------
cubic_norm = 4.*PI*integrate.quad(lambda x: x**2*cubic_W(x), 0, cubic_H)[0]
quartic_norm = 4.*PI*integrate.quad(lambda x: x**2*quartic_W(x), 0, quartic_H)[0]
wendland2_norm = 4.*PI*integrate.quad(lambda x: x**2*wendland2_W(x), 0, wendland2_H)[0]

print cubic_norm
print quartic_norm
print wendland2_norm


# Plot kernels ------------------------------------------
r = arange(0, 3.5*h, 1./100.)
xi = r/h

cubic_Ws = cubic_W(xi)
quartic_Ws = quartic_W(xi)
wendland2_Ws = wendland2_W(xi)



figure()

text(h-0.1, cubic_Ws[0]/20., "h", ha="right",va="center")
arrow(h, cubic_Ws[0]/10., 0., -cubic_Ws[0]/10., fc='k', ec='k', length_includes_head=True, head_length=cubic_Ws[0]/30., head_width=0.1)


plot(r,cubic_Ws, 'b-' ,label="Cubic")
plot(r, quartic_Ws, 'r-', label="Quartic")
plot(r, wendland2_Ws, 'g-', label="Wendland C2")

text(cubic_H-0.1, cubic_Ws[0]/20., "H", ha="right",va="center", color='b')
arrow(cubic_H, cubic_Ws[0]/10., 0., -cubic_Ws[0]/10., fc='b', ec='b', length_includes_head=True, head_length=cubic_Ws[0]/30., head_width=0.1)

text(quartic_H-0.1, cubic_Ws[0]/20., "H", ha="right",va="center", color='r')
arrow(quartic_H, cubic_Ws[0]/10., 0., -cubic_Ws[0]/10., fc='r', ec='r', length_includes_head=True, head_length=quartic_Ws[0]/30., head_width=0.1)

text(wendland2_H-0.1, cubic_Ws[0]/20., "H", ha="right",va="center", color='r')
arrow(wendland2_H, cubic_Ws[0]/10., 0., -cubic_Ws[0]/10., fc='g', ec='g', length_includes_head=True, head_length=wendland2_Ws[0]/30., head_width=0.1)


xlabel("r", labelpad=0)
ylabel("W(r,h)", labelpad=0)
legend(loc="upper right")
savefig("kernel.pdf")