# SPDX-License-Identifier: MIT """The steady-state test on directed-loop SSE runs of the Heisenberg model. Part 1 starts the spin-1/2 Heisenberg antiferromagnet on a 32 x 32 square lattice at T = 0.25 without thermalization, several times. The energy relaxes within the first one or two bins: a large but short transient. An outlier in the first bin alone cannot raise z above about sqrt(3), however large it is (see tutorial1c_test.py); larger values come from the second bin and the rest of the series. Part 2 repeats an equilibrated run many times and records how often the test passes. """ import numpy as np import matplotlib.pyplot as plt from scipy.stats import norm import pyalps import pyalps.hdf5 def steady_state_z(ts): """Return the fitted slope, its standard error and z = |slope| / error.""" ts = np.asarray(ts, dtype=float) N = len(ts) t = np.arange(1, N + 1) slope = np.polyfit(t, ts, 1)[0] error = np.std(ts, ddof=1) * np.sqrt(12. / (N * (N**2 - 1))) if error == 0.: return slope, error, 0. # a constant series, as in pyalps.checkSteadyState return slope, error, abs(slope) / error def cutoff(c): """z0 such that a standard normal Z satisfies P(|Z| < z0) = c.""" return norm.ppf((1. + c) / 2.) def result_files(prefix): """The result files in the order of the parameter list (task1, task2, ...).""" return sorted(pyalps.getResultFiles(prefix=prefix), key=lambda f: int(f.rsplit('.task', 1)[1].split('.')[0])) def binsize(outfile, observable): """The number of measurements in one time-series bin, as stored by ALPS.""" ar = pyalps.hdf5.archive(outfile.replace('.out.xml', '.out.h5'), 'r') return int(ar['/simulation/results/%s/timeseries/data/@binsize' % observable]) # Part 1: runs that start out of equilibrium. Without thermalization, # dirloop_sse needs INITIAL_CUTOFF, the initial length of the operator string. NSTART = 10 parms = [{ 'LATTICE' : "square lattice", 'MODEL' : "spin", 'local_S' : 0.5, 'L' : 32, 'J' : 1., 'T' : 0.25, 'THERMALIZATION' : 0, 'INITIAL_CUTOFF' : 40000, 'SWEEPS' : 2000, 'SEED' : seed, } for seed in range(1, NSTART + 1)] input_file = pyalps.writeInputFiles('parm1c_start_sse', parms) pyalps.runApplication('dirloop_sse', input_file, Tmin=1, writexml=True) files = result_files('parm1c_start_sse') series = [np.array(pyalps.loadTimeSeries(f, 'Energy')) for f in files] z = np.array([steady_state_z(ts)[2] for ts in series]) z_rest = np.array([steady_state_z(ts[1:])[2] for ts in series]) print('runs without thermalization: z =', ' '.join('%.1f' % v for v in z)) print('the same runs without the first bin: z =', ' '.join('%.1f' % v for v in z_rest)) for c in [0.6827, 0.95]: print('fraction passing at c = %g (z0 = %.2f): %.1f with the first bin, %.1f without' % (c, cutoff(c), np.mean(z < cutoff(c)), np.mean(z_rest < cutoff(c)))) # the run with the median z; each time-series entry is the sum of the # energy over one bin i = np.argsort(z)[len(z) // 2] ts = series[i] bins = binsize(files[i], 'Energy') print('first bin: %.0f above the mean of the other bins, %.0f times their scatter' % (ts[0] / bins - np.mean(ts[1:]) / bins, (ts[0] - np.mean(ts[1:])) / np.std(ts[1:]))) sweeps = (np.arange(len(ts)) + 0.5) * bins plt.figure() plt.plot(sweeps, ts / bins, 'o-', markersize=3) plt.xlabel('Monte Carlo sweeps (no thermalization)') plt.ylabel('Energy') plt.title('32 x 32 Heisenberg antiferromagnet, T = 0.25, SEED = %d: z = %.1f' % (parms[i]['SEED'], z[i])) plt.show() # Part 2: many equilibrated runs NRUNS = 100 c = 0.6827 parms = [{ 'LATTICE' : "square lattice", 'MODEL' : "spin", 'local_S' : 0.5, 'L' : 32, 'J' : 1., 'T' : 0.25, 'THERMALIZATION' : 1000, 'SWEEPS' : 4000, 'SEED' : seed, } for seed in range(1001, NRUNS + 1001)] input_file = pyalps.writeInputFiles('parm1c_equilibrated_sse', parms) pyalps.runApplication('dirloop_sse', input_file, Tmin=1, writexml=True) z = np.array([steady_state_z(pyalps.loadTimeSeries(f, 'Energy'))[2] for f in pyalps.getResultFiles(prefix='parm1c_equilibrated_sse')]) print('%d equilibrated runs, fraction passing at c = %g: %.2f (expected %.2f)' % (len(z), c, np.mean(z < cutoff(c)), c)) plt.figure() plt.hist(z, bins=np.linspace(0, 5, 26), density=True, histtype='step', lw=2, label='SSE energy') x = np.linspace(0, 5, 201) plt.plot(x, 2 * norm.pdf(x), 'k--', label='|Z|, Z standard normal') plt.axvline(cutoff(c), color='gray', lw=1) plt.xlabel('z = |slope| / error') plt.ylabel('probability density') plt.title('Equilibrated runs, 32 x 32 Heisenberg antiferromagnet, T = 0.25') plt.legend() plt.show()