Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/yang/blob/main/scripts/local_neighborhood.py # yang This project has not described itself yet. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `yang / scripts / local_neighborhood.py` 96 lines ยท 4410 bytes Raw Blame History 1 """Minimum combined area of two exchanges around fixed local Wilson plaquettes.""" 2 import numpy as np 3 4 from nested_face_cut import minimum_nested 5 6 7 def _grow(mask, faces, free): 8 """Closed one-step neighborhood in the free-link / plaquette incidence graph.""" 9 out = mask.copy() 10 touched = faces[mask[faces].any(axis=1)].ravel() 11 out[touched] = free[touched] 12 return out 13 14 15 def _endpoint(lat, free, site): 16 mask = np.zeros_like(free) 17 mask[lat.faces[lat.pairs.index((0, 1)), site]] = True 18 return mask & free 19 20 21 def _regions(lat, pins, target_site): 22 free = ~pins.ravel() 23 faces = lat.faces.reshape(-1, 4) 24 spatial = lat.faces[[i for i, pair in enumerate(lat.pairs) if 3 not in pair]].reshape(-1, 4) 25 source, target = (_endpoint(lat, free, site) for site in (0, target_site)) 26 for widen in (False, True): 27 left, right = (_grow(_grow(m, spatial, free) if widen else m, faces, free) 28 for m in (source, target)) 29 yield source, target, left, right 30 31 32 def neighborhood_masks(lat, pinned, *, report=False): 33 if lat.sites.size != 8**4: 34 raise ValueError('local measurement uses side eight') 35 pins = pinned.ravel() 36 faces = lat.faces.reshape(-1, 4) 37 free_on_face = ~pins[faces] 38 39 def split(mask): 40 return mask[faces].any(axis=1) & (free_on_face & ~mask[faces]).any(axis=1) 41 42 middle = [] 43 for kind, (source, target, left, right) in zip(('narrow', 'wide'), _regions(lat, pins, 4)): 44 small, big, cost = minimum_nested(faces, pins, left, right) 45 assert np.all(left <= small) and np.all(small <= big) and not (big & right).any() 46 assert not (big & pins).any() and not (left & right).any() 47 boundaries = [split(m) for m in (source, small, big, target)] 48 assert np.max(np.sum(boundaries, axis=0)) == 1 49 counts = [int(b.sum()) for b in boundaries] 50 assert counts[1]+counts[2] == cost 51 complement = ~pins & ~big 52 if report: 53 # Reversing the anchors selects a minimum from the opposite side. 54 # A disagreement exhibits nonuniqueness; equality is interpreted 55 # using residual reachability, not just two agreeing costs. 56 rev_small, rev_big, rev_cost = minimum_nested(faces, pins, right, left) 57 assert rev_cost == cost 58 free = ~pins 59 print(kind, 'threshold_links_differ_between_extremes', 60 int((small ^ (free & ~rev_big)).sum()), 61 int((big ^ (free & ~rev_small)).sum())) 62 # Sorting a pair hides which mask owns a height-one link. Two 63 # cofacial height-one links cannot have opposite owners: both 64 # unsorted masks would split their common face. 65 band = big & ~small 66 component = np.zeros_like(band) 67 if band.any(): 68 component[np.flatnonzero(band)[0]] = True 69 while True: 70 grown = _grow(component, faces, band) 71 if np.array_equal(component, grown): 72 break 73 component = grown 74 print(kind, 'height_one_component', int(component.sum()), 'of', int(band.sum())) 75 print(kind, 'protected', int(left.sum()), int(right.sum()), 'optimal_faces', counts, 76 'total', cost, 'source_side_links', int(small.sum()), 77 'target_side_links', int(complement.sum()), 'extra_links', 78 int((small & ~left).sum()), int((complement & ~right).sum())) 79 middle.extend((small, big)) 80 return (source, *middle, target) 81 82 83 if __name__ == '__main__': 84 from interior_geometry import _boundary, _tree_pins 85 from replica_shell_probe import Lattice 86 # The actual optimizations, and a constructive comparison at larger separation. 87 for side in (8, 10): 88 lat = Lattice(side, 'SU2') 89 pinned = _tree_pins(lat, side) 90 if side == 8: 91 neighborhood_masks(lat, pinned, report=True) 92 for name, (s, t, a, b) in zip(('narrow', 'wide'), _regions(lat, pinned, side//2)): 93 boundaries = [_boundary(lat, pinned, m) for m in (s, a, ~pinned.ravel() & ~b, t)] 94 assert not (a & b).any() and np.max(np.sum(boundaries, axis=0)) == 1 95 print('collars', side, name, 'protected', int(a.sum()), int(b.sum()), 96 'faces', [int(row.sum()) for row in boundaries])