-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffusion.py
More file actions
178 lines (161 loc) · 4.64 KB
/
diffusion.py
File metadata and controls
178 lines (161 loc) · 4.64 KB
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
import numpy as np
import matplotlib.pyplot as plt
import scipy.sparse.linalg
np.random.seed(27)
def get_adjacency_size(H, W):
"""
Each of the H x W nodes participates
in two edges (amortized). Lose (H + W)
edges to boundaries.
"""
m = H * W
n = H * W * 2 - (H + W)
return m, n
def make_hori_conn(i, j):
"""Connect (i, j) with (i, j-1)."""
H, W = i.shape
src = (i[:, 1:] * W + j[:, 1:]).ravel()
dst = (i[:, 1:] * W + j[:, 1:] - 1).ravel()
rng = np.arange(H * W - W, 2 * H * W - W - H).ravel()
return src, dst, rng
def make_vert_conn(i, j):
"""Connect (i, j) with (i-1, j)."""
H, W = i.shape
src = (i[1:, :] * W + j[1:, :]).ravel()
dst = ((i[1:, :] - 1) * W + j[1:, :]).ravel()
rng = np.arange(0, H * W - W).ravel()
return src, dst, rng
def make_conn(H, W):
"""Connect each node with its
neighbors up and to left."""
i, j = np.mgrid[:H, :W]
h_src, h_dst, h_rng = make_hori_conn(i, j)
v_src, v_dst, v_rng = make_vert_conn(i, j)
src = np.r_[h_src, v_src]
dst = np.r_[h_dst, v_dst]
rng = np.r_[h_rng, v_rng]
return src, dst, rng
def init_sparse_adjacency(H, W):
"""Use lil_matrix for efficient sparse update."""
m, n = get_adjacency_size(H, W)
A = scipy.sparse.lil_matrix((m, n))
return A
def make_adjacency(H, W):
"""
All cols sum to zero and
have two nonzero entries.
"""
A = init_sparse_adjacency(H, W)
src, dst, rng = make_conn(H, W)
A[src, rng] = -1
A[dst, rng] = +1
return A.tocsr()
def make_resistance(n, rho=1):
"""
For simplicity assume resistance
matrix multiple of identity.
"""
R = scipy.sparse.spdiags(np.full(n, rho), 0, n, n)
return R
def get_slice(L, maxlength, margin=10):
"""
Apply magic heuristic to get interesting
non-degenerate demo. Best not to ask.
"""
v0 = np.random.randint(L // margin, L)
v1 = v0 + np.random.randint(min(L - v0, maxlength))
return slice(v0, v1)
def get_fixed_node_mask(H, W, k=3, maxwidth=30, maxheight=10):
"""
True where node potential held fixed.
"""
mask = np.full((H, W), False)
for _ in range(k):
r = get_slice(H, maxheight)
c = get_slice(W, maxwidth)
mask[r, c] = True
return mask
def masked_scatter(size, val, mask, default=0):
"""
Combined fill and masked scatter.
"""
arr = np.full(size, default)
arr[mask] = val
return arr
def make_fixed_node_mats(H, W, fixed_val=1):
"""
Matrices B and C enforce fixed node
and source/sink constraints.
"""
m, n = get_adjacency_size(H, W)
mask = get_fixed_node_mask(H, W).ravel()
B = scipy.sparse.lil_matrix((m, m))
C = scipy.sparse.lil_matrix((m, m))
d = masked_scatter(m, fixed_val, mask)
B[~mask, ~mask] = 1
C[mask, mask] = 1
return B, C, d
def make_blocksys(A, R, B, C):
"""
Make block 3x3 system for solving:
A f + I s + 0 e = 0
R f + 0 s + A.T e = 0
0 f + B s + C e = d
In the stacked variable (f, s, e).
"""
m, n = A.shape
I_mxm = scipy.sparse.eye(m)
Z_mxm = scipy.sparse.csr_matrix((m, m))
Z_nxm = scipy.sparse.csr_matrix((n, m))
Z_mxn = scipy.sparse.csr_matrix((m, n))
blocksys = scipy.sparse.bmat([
[A, I_mxm, Z_mxm],
[R, Z_nxm, A.T],
[Z_mxn, B, C],
]).tocsr()
return blocksys
def make_problem(H, W):
"""
Construct problem instance for
diffusion in grid network.
"""
m, n = get_adjacency_size(H, W)
A = make_adjacency(H, W)
R = make_resistance(n)
B, C, d = make_fixed_node_mats(H, W)
blocksys = make_blocksys(A, R, B, C)
rhs = np.r_[np.zeros(m), np.zeros(n), d]
return blocksys, rhs
def how_sparse(mat):
"""Summarize problem size and structure."""
m, n = mat.shape
pct = mat.nnz / (m * n)
msg = f'Linear system: {m} eqns in {n} vars.\n'
msg += f'Sparsity: {pct:.010f}.'
print(msg)
def viz_diffusion(node_potentials, H, W):
"""Plot realized node potential as grid."""
img = node_potentials.reshape(H, W)
fig, ax = plt.subplots(figsize=(4, 4))
ax.imshow(img)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
fig.savefig('./diffusion.png',
bbox_inches='tight',
pad_inches=0,
dpi=200,
)
def main():
"""
Can comfortably push to 500x500
on intel i7 w/ 8GB RAM.
"""
H, W = 500, 500
m, n = get_adjacency_size(H, W)
blocksys, rhs = make_problem(H, W)
fse = scipy.sparse.linalg.spsolve(blocksys, rhs)
f, s, e = np.split(fse, [m, m + n])
how_sparse(blocksys)
viz_diffusion(e, H, W)
if __name__ == '__main__':
main()