yang
This project has not described itself yet.
Brought by drakestapleton
yang / scripts / probe_open_collar.py
| 1 | """Coupled Wilson heat baths on an open SU(2) box with two fixed boundary conditions.""" |
| 2 | import argparse |
| 3 | from itertools import product |
| 4 | |
| 5 | import numpy as np |
| 6 | from replica_shell_probe import mul, inv |
| 7 | |
| 8 | |
| 9 | def conditional(staples, beta, rng): |
| 10 | """Common random numbers for normalized exp(beta Re(q*staple)) dHaar(q).""" |
| 11 | norm = np.linalg.norm(staples, axis=-1) |
| 12 | field = beta*norm |
| 13 | shape = norm.shape |
| 14 | done = np.zeros(shape, dtype=bool) |
| 15 | scalar = np.zeros(shape) |
| 16 | while not done.all(): |
| 17 | u, accept = rng.random((2, shape[1])) |
| 18 | # Truncated exponential proposal for the scalar component on [-1,1]. |
| 19 | proposed = np.broadcast_to(2*u-1, shape).copy() |
| 20 | numerator = np.log1p((1-u)*np.expm1(-2*field)) |
| 21 | np.divide(numerator, field, out=proposed, where=field>0) |
| 22 | proposed = np.where(field>0, 1+proposed, proposed).clip(-1,1) |
| 23 | take = ~done & (accept**2 <= 1-proposed**2) |
| 24 | scalar[take] = proposed[take] |
| 25 | done |= take |
| 26 | direction = rng.normal(size=(shape[1],3)) |
| 27 | direction /= np.linalg.norm(direction, axis=-1, keepdims=True) |
| 28 | q = np.concatenate([scalar[...,None], np.sqrt(1-scalar**2)[...,None]*direction],axis=-1) |
| 29 | pole = np.zeros_like(staples) |
| 30 | pole[...,0] = 1 |
| 31 | np.divide(inv(staples), norm[...,None], out=pole, where=field[...,None]>0) |
| 32 | return mul(q,pole) |
| 33 | |
| 34 | |
| 35 | class Box: |
| 36 | def __init__(self, side): |
| 37 | self.side = side |
| 38 | shape = (side,)*4 |
| 39 | coords = np.indices(shape).reshape(4,-1) |
| 40 | directions = np.eye(4,dtype=int) |
| 41 | self.plus = np.array([np.ravel_multi_index((coords+d[:,None])%side,shape) for d in directions]) |
| 42 | self.minus = np.argsort(self.plus,axis=1) |
| 43 | self.classes = [] |
| 44 | for axis in range(4): |
| 45 | # An edge on a boundary hyperplane is fixed. No wrapped link is used. |
| 46 | other = np.delete(coords,axis,axis=0) |
| 47 | active = (coords[axis]<side-1) & ((other>0)&(other<side-1)).all(axis=0) |
| 48 | for parity in range(2): |
| 49 | self.classes.append((axis,np.flatnonzero(active & (coords.sum(axis=0)%2==parity)))) |
| 50 | mid=side//2 |
| 51 | self.center=np.array([np.ravel_multi_index((mid-1,mid-1,z,t),shape) |
| 52 | for z,t in product((mid-1,mid),repeat=2)]) |
| 53 | self.changed=np.ravel_multi_index((mid-1,mid-1,mid-1,0),shape) |
| 54 | |
| 55 | def sweep(self, u, beta, rng): |
| 56 | for i,x in self.classes: |
| 57 | total = np.zeros_like(u[:,i,x]) |
| 58 | for j in range(4): |
| 59 | if i==j: |
| 60 | continue |
| 61 | back=self.minus[j,x] |
| 62 | total += mul(u[:,j,self.plus[i,x]], inv(mul(u[:,j,x],u[:,i,self.plus[j,x]]))) |
| 63 | total += mul(inv(mul(u[:,i,back],u[:,j,self.plus[i,back]])),u[:,j,back]) |
| 64 | u[:,i,x] = conditional(total,beta,rng) |
| 65 | |
| 66 | def observe(self, u): |
| 67 | x=self.center |
| 68 | return mul(mul(u[:,0,x],u[:,1,self.plus[0,x]]), |
| 69 | inv(mul(u[:,1,x],u[:,0,self.plus[1,x]])))[...,0].mean(axis=1) |
| 70 | |
| 71 | def initial_collar(self, name): |
| 72 | # Only tangential links on a boundary hyperplane belong to the collar. |
| 73 | # The unused periodic wrap entries stay at the identity. |
| 74 | shape = (self.side,)*4 |
| 75 | coords = np.indices(shape).reshape(4, -1) |
| 76 | u = np.zeros((4, self.side**4, 4)) |
| 77 | u[..., 0] = 1 |
| 78 | if name == "identity": |
| 79 | return u |
| 80 | if name == "sheet": |
| 81 | mid = self.side // 2 |
| 82 | cut = (coords[0] == mid-1) & (coords[1] >= mid) & (coords[3] == 0) |
| 83 | u[0, cut, 0] = -1 |
| 84 | elif name == "rough": |
| 85 | rng = np.random.default_rng([self.side, 230, 314159]) |
| 86 | for axis in range(4): |
| 87 | tangential = np.delete(coords, axis, axis=0) |
| 88 | mask = (coords[axis] < self.side-1) & ((tangential == 0) | (tangential == self.side-1)).any(axis=0) |
| 89 | values = rng.normal(size=(mask.sum(), 4)) |
| 90 | u[axis, mask] = values / np.linalg.norm(values, axis=1, keepdims=True) |
| 91 | else: |
| 92 | raise ValueError(f"unknown collar {name}") |
| 93 | return u |
| 94 | |
| 95 | def pair(self, beta, seed, burn, keep, identical, collar="identity"): |
| 96 | rng=np.random.default_rng([self.side,round(100*beta),seed,261123]) |
| 97 | u = np.repeat(self.initial_collar(collar)[None], 2, axis=0) |
| 98 | if seed%2: |
| 99 | hot=rng.normal(size=(4,self.side**4,4)) |
| 100 | hot/=np.linalg.norm(hot,axis=-1,keepdims=True) |
| 101 | for i,x in self.classes: |
| 102 | u[:,i,x]=hot[i,x] |
| 103 | if not identical: |
| 104 | u[1,0,self.changed] *= -1 |
| 105 | pinned = u[:,0,self.changed].copy() |
| 106 | for _ in range(burn): |
| 107 | self.sweep(u,beta,rng) |
| 108 | rows=[] |
| 109 | for _ in range(keep): |
| 110 | self.sweep(u,beta,rng) |
| 111 | rows.append(self.observe(u)) |
| 112 | if identical and not np.array_equal(u[0],u[1]): |
| 113 | raise ValueError("identical collars diverged") |
| 114 | if not np.array_equal(u[:,0,self.changed], pinned): |
| 115 | raise ValueError("fixed collar moved") |
| 116 | sample=np.array(rows) |
| 117 | diff=sample[:,1]-sample[:,0] |
| 118 | return [*sample.mean(axis=0), diff.mean(), np.abs(diff).mean()] |
| 119 | |
| 120 | |
| 121 | if __name__ == "__main__": |
| 122 | p=argparse.ArgumentParser(description=__doc__) |
| 123 | p.add_argument("side",type=int,choices=(4,6)) |
| 124 | p.add_argument("beta",type=float,choices=(0,2,2.3)) |
| 125 | p.add_argument("seed",type=int) |
| 126 | p.add_argument("--burn",type=int,default=64) |
| 127 | p.add_argument("--keep",type=int,default=128) |
| 128 | p.add_argument("--identical",action="store_true") |
| 129 | a=p.parse_args() |
| 130 | v=Box(a.side).pair(a.beta,a.seed,a.burn,a.keep,a.identical) |
| 131 | print(a.side,a.beta,a.seed,a.burn,a.keep,int(a.identical),*(f"{y:.12g}" for y in v),sep="\t") |