|
| 1 | +import argparse |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import os |
| 4 | +import tqdm.auto as tqdm |
| 5 | +from scalar_yukawa.yukawa import * |
| 6 | + |
| 7 | +parser = argparse.ArgumentParser() |
| 8 | +parser.add_argument('--L', type=int, required=True) |
| 9 | +parser.add_argument('--Nd', type=int, required=True) |
| 10 | +parser.add_argument('--m2', type=float, required=True, help='Scalar mass squared') |
| 11 | +parser.add_argument('--lam', type=float, required=True, help='Scalar phi^4 coupling') |
| 12 | +parser.add_argument('--g', type=float, required=True, help='Yukawa coupling') |
| 13 | +parser.add_argument('--M', type=float, required=True, help='Fermion mass') |
| 14 | +globals().update(vars(parser.parse_args())) |
| 15 | + |
| 16 | +apbc = True |
| 17 | +logdir = 'hmc_toy_logs' |
| 18 | + |
| 19 | +### TODO! Edit me. |
| 20 | + |
| 21 | + |
| 22 | +prefix = os.path.join(logdir, f'ens_M{M}_g{g}_m{"m" if m2 < 0 else ""}{abs(m2)}_lam{lam}_Npf{N_pf}_Nd{Nd}_L{L}_a{apbc:d}') |
| 23 | +action = ToyAction(m2, lam, M, g, apbc) |
| 24 | + phi = 0.1*np.random.normal(size=(1,) + (L,)*Nd)+0.1 |
| 25 | + varphi = 0.1*np.random.normal(size=(2,) + (L,)*Nd) |
| 26 | + x = np.concatenate((phi, varphi), axis=0) |
| 27 | + n_therm = 100 |
| 28 | + tau = 1.0 |
| 29 | + n_leap = 10 |
| 30 | + tot_acc = 0 |
| 31 | + ens = [] |
| 32 | + varphi_ens = [] |
| 33 | + Ss = [] |
| 34 | + for i in tqdm.tqdm(range(-n_therm, 10000)): |
| 35 | + x, S, acc = hmc_update(x, action, tau, n_leap, verbose=True) |
| 36 | + tot_acc += acc |
| 37 | + if i >= 0 and (i+1) % 10 == 0: |
| 38 | + ens.append(np.copy(x[0])) |
| 39 | + varphi_ens.append(np.copy(x[1:])) |
| 40 | + Ss.append(S) |
| 41 | + if i >= 0 and i % 10 == 0: |
| 42 | + tqdm.tqdm.write(f'Acc = {tot_acc/(i+1+n_therm)}') |
| 43 | + tqdm.tqdm.write(f'phi bar = {np.mean(x[0])}') |
| 44 | + # tqdm.tqdm.write(f'logp = {-exact_action.compute_action(x)}') |
| 45 | + |
| 46 | + ens = np.stack(ens, axis=0) |
| 47 | + ens_fname = f'{prefix}.npy' |
| 48 | + np.save(ens_fname, ens) |
| 49 | + varphi_ens = np.stack(varphi_ens, axis=0) |
| 50 | + varphi_ens_fname = f'{prefix}.varphi.npy' |
| 51 | + np.save(varphi_ens_fname, varphi_ens) |
| 52 | + S_fname = f'{prefix}.S.npy' |
| 53 | + np.save(S_fname, np.array(Ss)) |
| 54 | + assert ens[0].shape[-1] == L |
| 55 | + axes = tuple(range(1, len(ens.shape))) |
| 56 | + mag = np.mean(np.abs(ens), axis=axes) |
| 57 | + assert len(mag.shape) == 1 |
| 58 | + mag_fname = f'{prefix}.mag.npy' |
| 59 | + np.save(mag_fname, mag) |
| 60 | + |
| 61 | + phi0 = np.mean(ens, axis=axes) |
| 62 | + np.save(f'{prefix}.phi0.npy', phi0) |
| 63 | + rms_phi = np.sqrt(np.mean(ens**2, axis=axes)) |
| 64 | + rms_phi_fname = f'{prefix}.rms_phi.npy' |
| 65 | + np.save(rms_phi_fname, rms_phi) |
| 66 | + assert len(phi0.shape) == 1 |
| 67 | + assert len(rms_phi.shape) == 1 |
| 68 | + print('<phi> =', al.bootstrap(phi0, Nboot=100, f=al.rmean)) |
| 69 | + print('<|phi|> =', al.bootstrap(np.abs(phi0), Nboot=100, f=al.rmean)) |
| 70 | + print('RMS phi =', al.bootstrap(rms_phi, Nboot=100, f=al.rmean)) |
| 71 | + fig, ax = plt.subplots(1,2, figsize=(8,4)) |
| 72 | + ax[0].plot(phi0) |
| 73 | + ax[1].hist(phi0, bins=20) |
| 74 | + plt.show() |
| 75 | + |
| 76 | + ens = np.load(f'{prefix}.npy') |
| 77 | + |
| 78 | + all_C = [] |
| 79 | + all_Cphi = [] |
| 80 | + all_cond = [] |
| 81 | + for phi in ens: |
| 82 | + D = make_D(phi, M=M, g=g, apbc=apbc) |
| 83 | + Di = np.linalg.inv(D.todense()) |
| 84 | + all_cond.append(np.trace(Di, axis1=-1, axis2=-2)) |
| 85 | + C = np.zeros(L) |
| 86 | + for i in range(L**Nd): |
| 87 | + mx = [] |
| 88 | + j = i |
| 89 | + for mu in range(Nd): |
| 90 | + mx.insert(0, -(j%L)) |
| 91 | + j //= L |
| 92 | + mx = tuple(mx) |
| 93 | + Px = np.asarray(Di)[:,i].reshape((L,)*Nd) |
| 94 | + Px = np.roll(Px, mx, axis=tuple(range(len(Px.shape)))) |
| 95 | + axes = tuple(range(0,Nd-1)) |
| 96 | + C += np.sum(Px**2, axis=axes) / L**Nd |
| 97 | + # C = np.array([np.mean([Di[i,i - i%L + (i+t)%L]**2 for i in range(L**Nd)]) for t in range(L)]) |
| 98 | + Cphi = np.array([np.mean(phi * np.roll(phi, -t, axis=-1)) for t in range(L)]) |
| 99 | + all_C.append(C) |
| 100 | + all_Cphi.append(Cphi) |
| 101 | + all_cond = np.array(all_cond) |
| 102 | + cond_fname = f'{prefix}.cond.npy' |
| 103 | + np.save(cond_fname, all_cond) |
| 104 | + print('<bar{chi} chi> =', al.bootstrap(all_cond, Nboot=100, f=al.rmean)) |
| 105 | + all_C = al.bootstrap(np.array(all_C), Nboot=100, f=al.rmean) |
| 106 | + all_Cphi = al.bootstrap(np.array(all_Cphi), Nboot=100, f=al.rmean) |
| 107 | + m = np.log(np.abs(all_C[0][1]/all_C[0][L//4])) / (L//4 - 1) |
| 108 | + fig, ax = plt.subplots(1,3, figsize=(12,4)) |
| 109 | + al.add_errorbar(all_C, ax=ax[0], marker='o') |
| 110 | + ax[0].set_yscale('log') |
| 111 | + ax[1].plot(ens[-1]) |
| 112 | + al.add_errorbar(all_Cphi, ax=ax[2], marker='o') |
| 113 | + ax[2].set_yscale('log') |
| 114 | + return m |
| 115 | + |
| 116 | +Ms = [0.0] |
| 117 | +ms = [compute_mass(M) for M in Ms] |
| 118 | +# fig = plt.figure() |
| 119 | +# plt.plot(Ms, ms, marker='o', linestyle='-') |
| 120 | +plt.show() |
0 commit comments