-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsector.py
145 lines (104 loc) · 3.63 KB
/
sector.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
145
#
# this is going to be a lightweight version of sector
# just the stuff the sector class would need
# no solving whatsoever
#
# note: no-flux boundary conditions are assumed in both r and theta
from scipy.sparse import kron as spkron, lil_matrix, coo_matrix
from numpy import zeros, eye, linspace, diag, arange, tile
from numpy.linalg import norm
from pickable import pickable
from dmatrix import DMatrix, DDMatrix
from dview import dview
class sector(pickable):
parnames = ['nr', 'ntheta', 'nv', 'nsp', 'r', 'R', 'omega', 'arclength',
'a', 'b', 'e', 'f', 'jac', 'D', 'dummy']
def __init__(self, pars):
self.__dict__.update(pars)
self.data = zeros(self.shape3)
#
# geometry properties
#
@property
def ntot(self): return self.nr*self.ntheta*self.nv
@property
def shape1(self): return self.ntot
@property
def shape3(self): return (self.nr, self.ntheta, self.nv)
@property
def aspect(self): return float(self.ntheta)/float(self.nr)
# radii
@property
def r1(self): return self.r # smaller radius
@property
def r2(self): return self.r + self.R # larger radius
# angle
@property
def theta(self): return self.arclength/self.r2
# deltas
@property
def dr(self): return self.R /float(self.nr - 1)
@property
def dtheta(self): return self.theta/float(self.ntheta - 1)
#
# data properties
#
@property
def flat(self): return self.data.reshape(self.shape1)
@property
def u(self): return self.data[...,0]
@property
def v(self): return self.data[...,1]
@property
def pars(self): return dview(self.__dict__, self.parnames)
@property
def trans1(self): return self.drmatrix() *self.flat
@property
def trans2(self): return self.dthetamatrix()*self.flat
def drmatrix(self):
dr = DMatrix(self.nr, self.R, False, self.nsp)
return spkron(spkron(dr, eye(self.ntheta)), eye(self.nv))
def dthetamatrix(self):
dt = DMatrix(self.ntheta, self.theta, False, self.nsp)
return spkron(spkron(eye(self.nr), dt), eye(self.nv))
def lapmatrix(self):
""" computes and returns the laplacian matrix """
dr = DMatrix(self.nr, self.R, False, self.nsp)
drr = DDMatrix(self.nr, self.R, False, self.nsp)
dtt = DDMatrix(self.ntheta, self.theta, False, self.nsp)
radii = linspace(self.r1, self.r2, self.nr, endpoint=True)
# one over r factor in front of the first radial derivative
dr = dr.todense()
for (row, r) in zip(dr, radii):
row /= r
dr = lil_matrix(dr)
r = radii
l1 = spkron(drr, eye(self.ntheta))
l2 = spkron(dr, eye(self.ntheta))
l3 = spkron(diag(1.0/r/r, 0), dtt)
return spkron(l1+l2+l3, self.D)
def rhs(self):
dtheta = self.dthetamatrix() * self.flat
lap = self.lapmatrix() * self.flat
f = (self.f(self.data, self.pars)).reshape(self.shape1)
return f + self.omega*dtheta + lap
def jacobian(self):
Dtheta = self.dthetamatrix()
lap = self.lapmatrix()
mat = lap + self.omega*Dtheta
j = self.jac(self.data, self.pars).transpose((2, 3, 0, 1))
n = self.nv*self.nv*self.nr*self.ntheta
j = j.reshape(n)
j = coo_matrix((j, self.jacindices()))
return (mat + j).tolil()
def jacindices(self):
""" computes indices of the jacobian of the kinetics """
nv, nr, ntheta = self.nv, self.nr, self.ntheta
coi = arange(nv).repeat(nv)
coj = tile(arange(nv), nv)
coI = (arange(nr*ntheta)*nv).repeat(nv*nv)
coJ = (arange(nr*ntheta)*nv).repeat(nv*nv)
for i in xrange(len(coi)): ## len(coi) == nv*nv !
coI[i::nv*nv] += coi[i]
coJ[i::nv*nv] += coj[i]
return coI, coJ