152def build(case, scheme, tau_mode="fit"):
153 N, h = case.N, case.h
155 muA, muB = case.muA, case.muB
156 ex, ey = np.array([1.0, 0.0]), np.array([0.0, 1.0])
157 n, t = case.n, case.t
159 C = np.zeros((N + 2, N + 2))
160 for i
in range(-1, N + 1):
161 for j
in range(-1, N + 1):
162 x0, y0 = i * h, j * h
163 ph = [case.phi(x0, y0), case.phi(x0 + h, y0), case.phi(x0, y0 + h), case.phi(x0 + h, y0 + h)]
165 C[i + 1, j + 1] = 1.0
167 C[i + 1, j + 1] = 0.0
169 C[i + 1, j + 1] = case.frac_cell(x0, y0)
170 Cc =
lambda i, j: C[i + 1, j + 1]
171 mu_of =
lambda f: f * muA + (1 - f) * muB
172 R_of =
lambda f: f / muA + (1 - f) / muB
174 mulo, muhi = (muA, muB)
if lowA
else (muB, muA)
178 def cell_links(i, j):
180 return [(
"xx", 0, 0, (m.iu(i + 1, j), m.iu(i, j)), f), (
"yy", 1, 1, (m.iv(i, j + 1), m.iv(i, j)), f)]
182 def node_links(i, j):
183 thy = case.frac_line(i * h, j * h, ey,
"hat")
184 thx = case.frac_line(i * h, j * h, ex,
"hat")
185 return [(
"yu", 1, 0, (m.iu(i, j), m.iu(i, j - 1)), thy), (
"xv", 0, 1, (m.iv(i, j), m.iv(i - 1, j)), thx)]
188 mixed = np.zeros((N + 2, N + 2), bool)
189 for i
in range(-1, N + 1):
190 for j
in range(-1, N + 1):
191 mixed[i + 1, j + 1] = 0.0 < Cc(i, j) < 1.0
193 for di
in (-1, 0, 1):
194 for dj
in (-1, 0, 1):
195 band[1:-1, 1:-1] |= mixed[1 + di:N + 1 + di, 1 + dj:N + 1 + dj]
200 """tau_hat = mu_lo t.S_lo n with L_lo from an LS fit of all links in the 3x3 block (kinked model)."""
201 if (i, j)
in tauexpr:
202 return tauexpr[(i, j)]
204 for di
in (-1, 0, 1):
205 for dj
in (-1, 0, 1):
206 ci, cj = i + di, j + dj
207 if not (0 <= ci <= N - 1
and 0 <= cj <= N - 1):
209 for L
in cell_links(ci, cj):
210 rows.append(L); rhs.append(
None)
211 for ni
in range(i - 1, i + 3):
212 for nj
in range(j - 1, j + 3):
213 if not (0 <= ni <= N
and 0 <= nj <= N):
215 for L
in node_links(ni, nj):
216 rows.append(L); rhs.append(
None)
217 A = np.zeros((len(rows), 4))
218 for r, (_, a, c, _, thA)
in enumerate(rows):
219 thlo = thA
if lowA
else 1 - thA
220 A[r, 2 * a + c] += 1.0
222 k = (1 - thlo) * n[a] * t[c] * (rr - 1)
225 A[r, 2 * p + q] += k * (t[p] * n[q] + t[q] * n[p])
226 G = np.linalg.pinv(A)
227 wv = np.array([[t[p] * n[q] + t[q] * n[p]
for q
in range(2)]
for p
in range(2)]).ravel()
228 coef = mulo * (wv @ G)
229 nn = np.array([[n[p] * n[q]
for q
in range(2)]
for p
in range(2)]).ravel()
231 wl = np.array([(1 - thA)
if lowA
else thA
for (_, _, _, _, thA)
in rows]) + DNN_EPS
232 cdn = nn @ (np.linalg.pinv(A * wl[:,
None]) * wl[
None, :])
236 for r, (_, a, c, (ip, im), _)
in enumerate(rows):
237 expr[ip] = expr.get(ip, 0.0) + coef[r] / h
238 expr[im] = expr.get(im, 0.0) - coef[r] / h
239 dexpr[ip] = dexpr.get(ip, 0.0) + cdn[r] / h
240 dexpr[im] = dexpr.get(im, 0.0) - cdn[r] / h
241 tauexpr[(i, j)] = expr
242 dnnexpr[(i, j)] = dexpr
246 if TAU_RULE ==
"owner":
247 cand = [(ci, cj)
for (ci, cj)
in cells
if 0 <= ci <= N - 1
and 0 <= cj <= N - 1
and mixed[ci + 1, cj + 1]]
249 cand = [(ci, cj)
for (ci, cj)
in cells
if 0 <= ci <= N - 1
and 0 <= cj <= N - 1]
250 own = min(cand, key=
lambda q: (abs(case.phi((q[0] + 0.5) * h, (q[1] + 0.5) * h)), q))
252 if TAU_RULE ==
"mixed-avg":
253 cs = [(ci, cj)
for (ci, cj)
in cells
if 0 <= ci <= N - 1
and 0 <= cj <= N - 1
and mixed[ci + 1, cj + 1]]
257 for k, v
in tau_fit(*cc).items():
258 e[k] = e.get(k, 0.0) + v / len(cs)
260 cs = [(ci, cj)
for (ci, cj)
in cells
if 0 <= ci <= N - 1
and 0 <= cj <= N - 1
and band[ci + 1, cj + 1]]
262 cs = [(ci, cj)
for (ci, cj)
in cells
if 0 <= ci <= N - 1
and 0 <= cj <= N - 1]
265 for k, v
in tau_fit(*cc).items():
266 e[k] = e.get(k, 0.0) + v / len(cs)
270 def stress(links, beta, factor, cells_adj):
271 """sigma = factor*[ beta*sum m + sum_l T_l (1 - beta Rbar_l) ] as (dict, const)."""
273 for (nm, a, c, (ip, im), thA)
in links:
274 if OWN_ONLY
is not None and nm
not in OWN_ONLY:
276 e[ip] = e.get(ip, 0.0) + factor * beta / h
277 e[im] = e.get(im, 0.0) - factor * beta / h
278 if scheme ==
"sharp":
279 for (_, a, c, (ip, im), thA)
in links:
280 k = factor * n[a] * t[c] * (1.0 - beta * R_of(thA))
281 if abs(k) < 1e-300
or thA
in (0.0, 1.0)
and abs(1.0 - beta * R_of(thA)) < 1e-12:
283 if tau_mode ==
"oracle":
284 const += k * case.tau_exact()
286 for kk, v
in tau_at(cells_adj).items():
287 e[kk] = e.get(kk, 0.0) + k * v
290 def stress_linkh(links, factor):
292 for (_, a, c, (ip, im), thA)
in links:
294 e[ip] = e.get(ip, 0.0) + factor * b / h
295 e[im] = e.get(im, 0.0) - factor * b / h
299 return muA
if case.phi(x, y) < 0
else muB
301 def beta_cell(i, j, row, side=None):
305 return mu_of(Cc(i, j))
306 if scheme ==
"facef" or (scheme ==
"sharp" and SHARP_BASE ==
"facef"):
307 xc, yc = (i + 0.5) * h, (j + 0.5) * h
308 return mu_of(case.frac_line(xc, yc, ey
if row ==
"u" else ex,
"box"))
309 return mu_of(Cc(i, j))
311 def beta_node(i, j, row, side=None):
314 cells = [Cc(i - 1, j - 1), Cc(i, j - 1), Cc(i - 1, j), Cc(i, j)]
315 mus = [mu_of(f)
for f
in cells]
316 if scheme ==
"harm4":
317 return 4.0 / sum(1.0 / x
for x
in mus)
318 if scheme ==
"facef" or (scheme ==
"sharp" and SHARP_BASE ==
"facef"):
319 return mu_of(case.frac_line(i * h, j * h, ex
if row ==
"u" else ey,
"box"))
320 return 0.25 * sum(mus)
323 GFM = scheme ==
"sharp" and SHARP_BASE ==
"gfm"
325 def sig_cell(i, j, comp, side=None):
326 key = (
"c", i, j, comp, side)
328 L = cell_links(i, j)[comp]
329 if scheme ==
"linkh":
330 cache[key] = stress_linkh([L], 2.0)
332 cache[key] = stress([L], beta_cell(i, j,
"u" if comp == 0
else "v", side), 2.0, [(i, j)])
335 def sig_node(i, j, row, side=None):
336 key = (
"n", i, j, row, side)
339 if OWN_ONLY
is not None:
340 L = [x
for x
in L
if x[0] == (
"yu" if row ==
"u" else "xv")]
341 if scheme ==
"linkh":
342 cache[key] = stress_linkh(L, 1.0)
344 cache[key] = stress(L, beta_node(i, j, row, side), 1.0, [(i - 1, j - 1), (i, j - 1), (i - 1, j), (i, j)])
349 for i
in range(N + 1):
350 for j
in range(-1, N + 1):
351 if m.unknown_u(i, j):
352 uidx[m.iu(i, j)] = len(uidx)
353 for i
in range(-1, N + 1):
354 for j
in range(N + 1):
355 if m.unknown_v(i, j):
356 uidx[m.iv(i, j)] = len(uidx)
359 ntot = nvel + npr + 1
360 pid =
lambda i, j: nvel + i * N + j
362 xall = np.zeros(m.nu + m.nv)
363 for i
in range(N + 1):
364 for j
in range(-1, N + 1):
365 xall[m.iu(i, j)] = case.face_avg(0, i * h, (j + 0.5) * h, ey)
366 for i
in range(-1, N + 1):
367 for j
in range(N + 1):
368 xall[m.iv(i, j)] = case.face_avg(1, (i + 0.5) * h, j * h, ex)
370 R, Cl, V = [], [], []
372 def add_J(row, ci, cj, w):
373 """row += w * J(cell), J = 2 (muB - muA) D_nn."""
374 if FCORR ==
"oracle":
375 dnn = case.eps
if case.kind
in (
"extension",
"general")
else 0.0
376 b[row] -= w * 2 * (muB - muA) * dnn
379 add(row, {k: w * 2 * (muB - muA) * v
for k, v
in dnnexpr[(ci, cj)].items()}, 0.0, 1.0)
382 def add(row, expr, const, sgn):
384 for k, v
in expr.items():
386 R.append(row); Cl.append(uidx[k]); V.append(sgn * v)
388 b[row] -= sgn * v * xall[k]
389 b[row] -= sgn * const
391 for i
in range(N + 1):
393 if not m.unknown_u(i, j):
396 sd = side_mu(i * h, (j + 0.5) * h)
if GFM
else None
397 e, c0 = sig_cell(i, j, 0, sd); add(r, e, c0, 1.0 / h)
398 e, c0 = sig_cell(i - 1, j, 0, sd); add(r, e, c0, -1.0 / h)
399 e, c0 = sig_node(i, j + 1,
"u", sd); add(r, e, c0, 1.0 / h)
400 e, c0 = sig_node(i, j,
"u", sd); add(r, e, c0, -1.0 / h)
401 R += [r, r]; Cl += [pid(i, j), pid(i - 1, j)]; V += [-1.0 / h, 1.0 / h]
404 for (ci, sg)
in ((i, 1.0), (i - 1, -1.0)):
405 xc, yc = (ci + 0.5) * h, (j + 0.5) * h
406 dq = case.frac_line(xc, yc, ey,
"box") - Cc(ci, j)
408 add_J(r, ci, j, sg * dq / h)
410 for j
in range(N + 1):
411 if not m.unknown_v(i, j):
414 sd = side_mu((i + 0.5) * h, j * h)
if GFM
else None
415 e, c0 = sig_node(i + 1, j,
"v", sd); add(r, e, c0, 1.0 / h)
416 e, c0 = sig_node(i, j,
"v", sd); add(r, e, c0, -1.0 / h)
417 e, c0 = sig_cell(i, j, 1, sd); add(r, e, c0, 1.0 / h)
418 e, c0 = sig_cell(i, j - 1, 1, sd); add(r, e, c0, -1.0 / h)
419 R += [r, r]; Cl += [pid(i, j), pid(i, j - 1)]; V += [-1.0 / h, 1.0 / h]
422 for (cj, sg)
in ((j, 1.0), (j - 1, -1.0)):
423 xc, yc = (i + 0.5) * h, (cj + 0.5) * h
424 dq = case.frac_line(xc, yc, ex,
"box") - Cc(i, cj)
426 add_J(r, i, cj, sg * dq / h)
430 div = {m.iu(i + 1, j): 1.0 / h, m.iu(i, j): -1.0 / h, m.iv(i, j + 1): 1.0 / h, m.iv(i, j): -1.0 / h}
431 add(r, div, 0.0, 1.0)
432 R.append(r); Cl.append(ntot - 1); V.append(1.0)
433 R.append(ntot - 1); Cl.append(r); V.append(1.0)
434 A = sp.csr_matrix((V, (R, Cl)), shape=(ntot, ntot))
436 build.last = dict(C=C, mixed=mixed, tauexpr=tauexpr, dnnexpr=dnnexpr, xall=xall, mac=m)
437 return A, b, uidx, xall, m