-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmatrix.py
144 lines (102 loc) · 2.85 KB
/
dmatrix.py
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
from numpy import diag, ones, zeros, array, arange
from scipy.sparse import lil_matrix
def weights(z, x):# , n, m):
""" this is the famous Fornberg's weights routine to compute the coeffs
of finite differences approximation.
Parameters:
z : the x-coordinate where the approximation is needed
x : the array of x-coordinates of the grid points """
n = len(x)-1
m = len(x)-1
c1 = 1.0
c4 = x[0]-z
c = zeros((n+1, m+1))
c[0,0] = 1.0
for i in xrange(1, n+1):
mn = min(i, m)
c2 = 1.0
c5 = c4
c4 = x[i]-z
for j in xrange(i):
c3 = x[i]-x[j]
c2 = c2*c3
if j == i-1:
for k in xrange(mn,0,-1):
c[i,k] = c1*(k*c[i-1,k-1] - c5*c[i-1,k])/c2
c[i,0] = -c1*c5*c[i-1,0]/c2
for k in xrange(mn,0,-1):
c[j,k] = (c4*c[j,k] - float(k)*c[j,k-1])/c3
c[j,0] = c4*c[j,0]/c3
c1 = c2
return array(zip(*c))
def dmatrix(n, order, nsp):
"""
returns a 1D finite-difference derivative matrix
assumes no-flux boundaries
Parameters:
n : number of points
order : order of the derivative
nsp : number of stencil points, must be odd
"""
m = zeros((n, n), dtype=float)
# first, fill the bulk of the matrix...
x = arange(nsp, dtype=float) - (nsp-1.0)/2.0
coefs = weights(0.0, x)[order]
for (c, i) in zip(coefs, map(int, x)):
m += c*diag(ones(n-abs(i)), i)
# ... then the corners
# imposing no-flux boundary conditions
x = arange(nsp, dtype=float) - 1.0
c0 = weights(0.0, x)[1]
for j in xrange((nsp-1)/2):
c = weights(float(j), x)[order]
c = c - c[0]/c0[0]*c0
r = m[j] # take the row of the matrix
r[0:nsp-1] = c[1:]
r = m[-1-j, -1::-1] # reverse of the minus -j-th row of the matrix
r[0:nsp-1] = c[1:]
return m
def dm(n, length, perbc=False, nsp=9):
m = dmatrix(n, 1, nsp)
dx = float(length)/float(n-1)
return lil_matrix(m/dx)
def ddm(n, length, perbc=False, nsp=9):
m = dmatrix(n, 2, nsp)
dx = float(length)/float(n-1)
return lil_matrix(m/dx/dx)
#
# simple versions
#
def dM(n, length, perbc=False, nsp=9):
m = -diag(ones(n))
m = m + diag(ones(n-1), 1)
m[0,0] = 0.0
m[0,1] = 0.0
m[-1,-1] = 0.0
m[-1,-2] = 0.0
dx = float(length)/float(n-1)
return lil_matrix(m/dx)
def dMplus(n, length, perbc=False, nsp=9):
m = -1.5*diag(ones(n))
m = m + 2.0*diag(ones(n-1), 1)
m = m - 0.5*diag(ones(n-2), 2)
m[0,0] = 0.0
m[0,1] = 0.0
m[0,2] = 0.0
m[-1,-1] = 0.0
m[-2,-1] = 0.0
m[-2,-2] = 1.5
m[-2,-3] =-2.0
m[-2,-4] = 0.5
dx = float(length)/float(n-1)
return lil_matrix(m/dx)
def ddM(n, length, perbc=False, nsp=9):
m = -2.0*diag(ones(n))
m = m + diag(ones(n-1), 1)
m = m + diag(ones(n-1), -1)
m[ 0, 1] = 2.0
m[-1,-2] = 2.0
dx = float(length)/float(n-1)
return lil_matrix(m/dx/dx)
# DMatrix, DDMatrix = dm, ddm
DMatrix, DDMatrix = dMplus, ddM