def main(N, cache_dir=None): '''Generate and test a problem with N spins''' if WIRE: h, J, gam = generate_wire(N) else: h, J, gam = generate_prob(N) t = time() if N < 18: print('Exact solution...') e_vals, e_vecs = solve(h, J, gamma=gam) print(e_vals[:2]) print('Runtime: {0:.3f} (s)'.format(time()-t)) t = time() solver = RP_Solver(h, J, gam, verbose=False, cache_dir=cache_dir) solver.solve() print('\n'*3) print('New RP solution:') print(solver.node.Es[:2]) print('Runtime: {0:.3f} (s)'.format(time()-t)) msolver = RP_Solver(h, J, gam) t = time() modes = solver.node.modes msolver.mode_solve(modes) print('\n'*3) print('Mode solved:') print(msolver.node.Es[:2]) print('Runtime: {0:.3f} (s)'.format(time()-t)) f = lambda x: np.round(x,2) print(solver.node.Hx.shape) print(msolver.node.Hx.shape) Dx = solver.node.Hx - msolver.node.Hx print('Dx diffs...') for i,j in np.transpose(np.nonzero(Dx)): print('\t{0}:{1} :: {2:.3f}'.format(i,j,Dx[i,j])) # resolve for _ in range(TRIALS): t = time() nx, nz = solver.ground_fields() solver = RP_Solver(h, J, gam, nx=nx, nz=nz, verbose=False, cache_dir=cache_dir) solver.solve() print('\n'*3) print('New RP solution:') print(solver.node.Es[:2]) print('Runtime: {0:.3f} (s)'.format(time()-t))
def new_gamma_sweep(N, gmax=10.): ''' ''' rp_times = [] sp_times = [] N_steps = 50 gammas = np.linspace(1e-5,1, N_steps) eps = np.linspace(1,1e-5, N_steps) h, J = gen_wire_coefs(N) for i, (gam,ep) in enumerate(zip(gammas,eps)): sys.stdout.write('\r{0:.1f}%: {1}'.format(i*100/N_steps, i)) sys.stdout.flush() t = time() for _ in range(TRIALS): sys.stdout.write('.') sys.stdout.flush() solver = RP_Solver(ep*h, ep*J, gam) solver.solve() rp_times.append((time()-t)/TRIALS) print(rp_times[-1]) if N <= SP_MAX: t = time() for _ in range(TRIALS): sys.stdout.write(',') sys.stdout.flush() e_vals, e_vecs = solve(ep*h, ep*J, gamma=gam) sp_times.append((time()-t)/TRIALS) plt.figure('Gamma-Sweep') plt.plot(gammas, rp_times, 'g', linewidth=LW) if sp_times: plt.plot(gammas, sp_times, 'b', linewidth=LW) plt.xlabel('s', fontsize=GS) plt.ylabel('Run-time (s)', fontsize=FS) plt.text(0.28, .6, 'H = s$H_T$ + (1-s) $H_P$', fontsize=20) if SAVE: plt.savefig(os.path.join(IMG_DIR, 'rp_wire_gamma_{0}.eps'.format(N)), bbox_inches='tight') plt.show()
def wire_size_sweep(N_max): '''compute ground and first excited band for up to N_max length wires''' rp_times = {'naive': [], 'local': [], 'global': []} sp_times = [] Ns = np.arange(2, N_max + 1, int(np.ceil((N_max - 1) * 1. / 40))) for N in Ns: sys.stdout.write('\r{0:.1f}%: {1}'.format((N - 1) * 100 / (N_max - 1), N)) sys.stdout.flush() h, J = gen_wire_coefs(N) for k in rp_times: if k == 'naive': chdir = None elif k == 'global': chdir = CACHE elif k == 'local': chdir = os.path.join(CACHE, str(N)) t = time() if False: e_vals, e_vecs, modes = rp_solve(h, J, gam=0.01, cache_dir=chdir) else: solver = RP_Solver(h, J, gam=0, cache_dir=chdir) solver.solve() rp_times[k].append(time() - t) if N <= 0: t = time() e_vals, e_vecs = solve(h, J, gamma=0.1) sp_times.append(time() - t) plt.figure('Run-times') plt.plot(Ns, rp_times['naive'], 'b', linewidth=2) plt.plot(Ns, rp_times['local'], 'g', linewidth=2) plt.plot(Ns, rp_times['global'], 'r', linewidth=2) if sp_times: plt.plot(Ns[:len(sp_times)], sp_times, 'g', linewidth=2) plt.xlabel('Wire length') plt.ylabel('Run-time (s)') plt.legend(['Naive', 'Local', 'Global'], fontsize=FS) plt.show() if SAVE: plt.savefig(os.path.join(IMG_DIR, 'rp_wire_{0}.eps'.format(N_max)), bbox_inches='tight')
def new_gamma_sweep(N, gmax=10.): ''' ''' rp_times = [] sp_times = [] N_steps = 50 gammas = np.linspace(1e-5, 1, N_steps) eps = np.linspace(1, 1e-5, N_steps) h, J = gen_wire_coefs(N) for i, (gam, ep) in enumerate(zip(gammas, eps)): sys.stdout.write('\r{0:.1f}%: {1}'.format(i * 100 / N_steps, i)) sys.stdout.flush() t = time() for _ in range(TRIALS): sys.stdout.write('.') sys.stdout.flush() solver = RP_Solver(ep * h, ep * J, gam) solver.solve() rp_times.append((time() - t) / TRIALS) print(rp_times[-1]) if N <= SP_MAX: t = time() for _ in range(TRIALS): sys.stdout.write(',') sys.stdout.flush() e_vals, e_vecs = solve(ep * h, ep * J, gamma=gam) sp_times.append((time() - t) / TRIALS) plt.figure('Gamma-Sweep') plt.plot(gammas, rp_times, 'g', linewidth=LW) if sp_times: plt.plot(gammas, sp_times, 'b', linewidth=LW) plt.xlabel('s', fontsize=GS) plt.ylabel('Run-time (s)', fontsize=FS) plt.text(0.28, .6, 'H = s$H_T$ + (1-s) $H_P$', fontsize=20) if SAVE: plt.savefig(os.path.join(IMG_DIR, 'rp_wire_gamma_{0}.eps'.format(N)), bbox_inches='tight') plt.show()
def wire_spectrum(N, gmin=0.01, gmax=2.): '''Calculate spectrum of an N cell wire for a gamma sweep''' gammas = np.linspace(gmin, gmax, 20) h, J = gen_wire_coefs(N) spectrum = [] times = [] for i, gamma in enumerate(gammas): sys.stdout.write('\r{0:.1f}%'.format( (i + 1) * 100. / (gammas.size + 1))) sys.stdout.flush() t = time() solver = RP_Solver(h, J, gamma) solver.solve() e_vals, e_vecs = solver.node.evd() # e_vals, e_vecs, modes = rp_solve(h, J, gam=gamma, cache_dir=CACHE) times.append(time() - t) spectrum.append(e_vals) # make square L = min(len(s) for s in spectrum) spectrum = [s[:L] for s in spectrum] spectrum = np.array(spectrum) plt.figure('Wire spectrum') grnd = anlyt_grnd(N, gammas) for n in range(0, int(np.log2(N))): gaps = anlyt_gaps(N, gammas, n) for gap in gaps: plt.plot(gammas, grnd + gap, 'k-', linewidth=2) plt.plot(gammas, spectrum, 'x', markersize=7, markeredgewidth=2) plt.xlabel('Gamma', fontsize=FS) plt.ylabel('Energy', fontsize=FS) plt.title('{0} cell wire spectrum'.format(N), fontsize=FS) plt.show(block=False) plt.figure('Timer') plt.plot(gammas, times, 'x', markersize=5, markeredgewidth=1.5) plt.xlabel('Gamma', fontsize=FS) plt.ylabel('Runtime (s)', fontsize=FS) plt.title('{0} cell wire spectrum: runtime'.format(N), fontsize=FS) plt.show()
def wire_spectrum(N, gmin=0.01, gmax=2.): '''Calculate spectrum of an N cell wire for a gamma sweep''' gammas = np.linspace(gmin, gmax, 20) h, J = gen_wire_coefs(N) spectrum = [] times = [] for i, gamma in enumerate(gammas): sys.stdout.write('\r{0:.1f}%'.format((i+1)*100./(gammas.size+1))) sys.stdout.flush() t = time() solver = RP_Solver(h, J, gamma) solver.solve() e_vals, e_vecs = solver.node.evd() # e_vals, e_vecs, modes = rp_solve(h, J, gam=gamma, cache_dir=CACHE) times.append(time()-t) spectrum.append(e_vals) # make square L = min(len(s) for s in spectrum) spectrum = [s[:L] for s in spectrum] spectrum = np.array(spectrum) plt.figure('Wire spectrum') grnd = anlyt_grnd(N, gammas) for n in range(0, int(np.log2(N))): gaps = anlyt_gaps(N, gammas, n) for gap in gaps: plt.plot(gammas, grnd+gap, 'k-', linewidth=2) plt.plot(gammas, spectrum, 'x', markersize=7, markeredgewidth=2) plt.xlabel('Gamma', fontsize=FS) plt.ylabel('Energy', fontsize=FS) plt.title('{0} cell wire spectrum'.format(N), fontsize=FS) plt.show(block=False) plt.figure('Timer') plt.plot(gammas, times, 'x', markersize=5, markeredgewidth=1.5) plt.xlabel('Gamma', fontsize=FS) plt.ylabel('Runtime (s)', fontsize=FS) plt.title('{0} cell wire spectrum: runtime'.format(N), fontsize=FS) plt.show()
def wire_size_sweep(N_max): '''compute ground and first excited band for up to N_max length wires''' rp_times = {'naive': [], 'local': [], 'global': []} sp_times = [] Ns = np.arange(2, N_max+1, int(np.ceil((N_max-1)*1./40))) for N in Ns: sys.stdout.write('\r{0:.1f}%: {1}'.format((N-1)*100/(N_max-1), N)) sys.stdout.flush() h, J = gen_wire_coefs(N) for k in rp_times: if k=='naive': chdir = None elif k == 'global': chdir = CACHE elif k == 'local': chdir = os.path.join(CACHE, str(N)) t = time() if False: e_vals, e_vecs, modes = rp_solve(h, J, gam=0.01, cache_dir=chdir) else: solver = RP_Solver(h, J, gam=0, cache_dir=chdir) solver.solve() rp_times[k].append(time()-t) if N <= 0: t = time() e_vals, e_vecs = solve(h, J, gamma=0.1) sp_times.append(time()-t) plt.figure('Run-times') plt.plot(Ns, rp_times['naive'], 'b', linewidth=2) plt.plot(Ns, rp_times['local'], 'g', linewidth=2) plt.plot(Ns, rp_times['global'], 'r', linewidth=2) if sp_times: plt.plot(Ns[:len(sp_times)], sp_times, 'g', linewidth=2) plt.xlabel('Wire length') plt.ylabel('Run-time (s)') plt.legend(['Naive', 'Local', 'Global'], fontsize=FS) plt.show() if SAVE: plt.savefig(os.path.join(IMG_DIR, 'rp_wire_{0}.eps'.format(N_max)), bbox_inches='tight')
def run_rp2(self, rp_steps, caching, cache_dir): '''Compute the spectrum using the new RP-Solver''' print('\nRunning New RP-Solver method...') t = time() if caching: cache_dir = CACHE2 if cache_dir is None else cache_dir else: cache_dir = None spectrum = [] for i, (gam, ep) in enumerate(zip(self.gammas, self.eps)): sys.stdout.write('\r{0:.2f}%'.format(i * 100. / self.nsteps)) sys.stdout.flush() ep = max(ep, EPS_MIN) solver = RP_Solver(ep * self.h, ep * self.J, gam, cache_dir=cache_dir) solver.solve() e_vals, e_vecs = solver.node.evd() spectrum.append(list(e_vals)) return spectrum # split schedule into iso-field steps rp_steps = max(1, rp_steps) niso = int(np.ceil(len(self.gammas) * 1. / rp_steps)) isos = [] for i in range(rp_steps): gams = self.gammas[i * niso:(i + 1) * niso] eps = self.eps[i * niso:(i + 1) * niso] isos.append((gams, eps)) # solve spectrum within each iso-step spectrum = [] i = 0 for gammas, eps in isos: i += 1 # solve first problem is iso-step gam, ep = gammas[0], max(eps[0], EPS_MIN) solver = RP_Solver(ep * self.h, ep * self.J, gam, cache_dir=cache_dir) solver.solve() e_vals, e_vecs = solver.node.evd() spectrum.append(list(e_vals)) nx, nz = solver.get_current_fields() modes = solver.node.modes # solve remaining problems is iso-step for gam, ep in zip(gammas[1:], eps[1:]): i += 1 sys.stdout.write('\r{0:.2f}%'.format(i * 100. / self.nsteps)) sys.stdout.flush() ep = max(ep, EPS_MIN) solver = RP_Solver(ep * self.h, ep * self.J, gam, nx=nx, nz=nz, cache_dir=cache_dir) solver.mode_solve(modes) spectrum.append(list(solver.node.e_vals)) return spectrum
def new_wire_size_sweep(N_max): ''' ''' rp_times = [] rp_times2 = [] sp_times = [] sp_times2 = [] Ns = np.arange(2, N_max+1) for N in Ns: sys.stdout.write('\r{0:.1f}%: {1}'.format((N-1)*100/(N_max-1), N)) sys.stdout.flush() h, J = gen_wire_coefs(N) # rp solver t = time() for _ in range(TRIALS): sys.stdout.write(',') sys.stdout.flush() solver = RP_Solver(h, J, 0) solver.solve() rp_times.append((time()-t)/TRIALS) # rp solver with gamma=eps t = time() for _ in range(TRIALS): sys.stdout.write('.') sys.stdout.flush() solver = RP_Solver(h, J, 1) solver.solve() rp_times2.append((time()-t)/TRIALS) # exact solver if N <= SP_MAX: t = time() for _ in range(TRIALS): sys.stdout.write(',') sys.stdout.flush() e_vals, e_vecs = solve(h, J, gamma=0) sp_times.append((time()-t)/TRIALS) t = time() for _ in range(TRIALS): sys.stdout.write('.') sys.stdout.flush() e_vals, e_vecs = solve(h, J, gamma=1) sp_times2.append((time()-t)/TRIALS) # plotting plt.figure('Run-times') plt.plot(Ns, rp_times, 'g', linewidth=LW) plt.plot(Ns[:len(sp_times)], sp_times, 'b', linewidth=LW) plt.plot(Ns, rp_times2, 'g--', linewidth=LW) plt.plot(Ns[:len(sp_times2)], sp_times2, 'b--', linewidth=LW) plt.xlabel('Wire length', fontsize=FS) plt.ylabel('Run-time (s)', fontsize=FS) plt.legend(['RP-Solver', 'Exact: ARPACK'], fontsize=FS) if SAVE: plt.savefig(os.path.join(IMG_DIR, 'rp_wire_{0}.eps'.format(N_max)), bbox_inches='tight') plt.show()
def new_wire_size_sweep(N_max): ''' ''' rp_times = [] rp_times2 = [] sp_times = [] sp_times2 = [] Ns = np.arange(2, N_max + 1) for N in Ns: sys.stdout.write('\r{0:.1f}%: {1}'.format((N - 1) * 100 / (N_max - 1), N)) sys.stdout.flush() h, J = gen_wire_coefs(N) # rp solver t = time() for _ in range(TRIALS): sys.stdout.write(',') sys.stdout.flush() solver = RP_Solver(h, J, 0) solver.solve() rp_times.append((time() - t) / TRIALS) # rp solver with gamma=eps t = time() for _ in range(TRIALS): sys.stdout.write('.') sys.stdout.flush() solver = RP_Solver(h, J, 1) solver.solve() rp_times2.append((time() - t) / TRIALS) # exact solver if N <= SP_MAX: t = time() for _ in range(TRIALS): sys.stdout.write(',') sys.stdout.flush() e_vals, e_vecs = solve(h, J, gamma=0) sp_times.append((time() - t) / TRIALS) t = time() for _ in range(TRIALS): sys.stdout.write('.') sys.stdout.flush() e_vals, e_vecs = solve(h, J, gamma=1) sp_times2.append((time() - t) / TRIALS) # plotting plt.figure('Run-times') plt.plot(Ns, rp_times, 'g', linewidth=LW) plt.plot(Ns[:len(sp_times)], sp_times, 'b', linewidth=LW) plt.plot(Ns, rp_times2, 'g--', linewidth=LW) plt.plot(Ns[:len(sp_times2)], sp_times2, 'b--', linewidth=LW) plt.xlabel('Wire length', fontsize=FS) plt.ylabel('Run-time (s)', fontsize=FS) plt.legend(['RP-Solver', 'Exact: ARPACK'], fontsize=FS) if SAVE: plt.savefig(os.path.join(IMG_DIR, 'rp_wire_{0}.eps'.format(N_max)), bbox_inches='tight') plt.show()
def run_rp2(self, rp_steps, caching, cache_dir): '''Compute the spectrum using the new RP-Solver''' print('\nRunning New RP-Solver method...') t = time() if caching: cache_dir = CACHE2 if cache_dir is None else cache_dir else: cache_dir = None spectrum = [] for i, (gam, ep) in enumerate(zip(self.gammas, self.eps)): sys.stdout.write('\r{0:.2f}%'.format(i*100./self.nsteps)) sys.stdout.flush() ep = max(ep, EPS_MIN) solver = RP_Solver(ep*self.h, ep*self.J, gam, cache_dir=cache_dir) solver.solve() e_vals, e_vecs = solver.node.evd() spectrum.append(list(e_vals)) return spectrum # split schedule into iso-field steps rp_steps = max(1, rp_steps) niso = int(np.ceil(len(self.gammas)*1./rp_steps)) isos = [] for i in range(rp_steps): gams = self.gammas[i*niso:(i+1)*niso] eps = self.eps[i*niso:(i+1)*niso] isos.append((gams, eps)) # solve spectrum within each iso-step spectrum = [] i = 0 for gammas, eps in isos: i += 1 # solve first problem is iso-step gam, ep = gammas[0], max(eps[0], EPS_MIN) solver = RP_Solver(ep*self.h, ep*self.J, gam, cache_dir=cache_dir) solver.solve() e_vals, e_vecs = solver.node.evd() spectrum.append(list(e_vals)) nx, nz = solver.get_current_fields() modes = solver.node.modes # solve remaining problems is iso-step for gam, ep in zip(gammas[1:], eps[1:]): i += 1 sys.stdout.write('\r{0:.2f}%'.format(i*100./self.nsteps)) sys.stdout.flush() ep = max(ep, EPS_MIN) solver = RP_Solver(ep*self.h, ep*self.J, gam, nx=nx, nz=nz, cache_dir=cache_dir) solver.mode_solve(modes) spectrum.append(list(solver.node.e_vals)) return spectrum
def main(N, cache_dir=None): '''Generate and test a problem with N spins''' if WIRE: h, J, gam = generate_wire(N) else: h, J, gam = generate_prob(N) t = time() if N < 18: print('Exact solution...') e_vals, e_vecs = solve(h, J, gamma=gam) print(e_vals[:2]) print('Runtime: {0:.3f} (s)'.format(time() - t)) t = time() solver = RP_Solver(h, J, gam, verbose=False, cache_dir=cache_dir) solver.solve() print('\n' * 3) print('New RP solution:') print(solver.node.Es[:2]) print('Runtime: {0:.3f} (s)'.format(time() - t)) msolver = RP_Solver(h, J, gam) t = time() modes = solver.node.modes msolver.mode_solve(modes) print('\n' * 3) print('Mode solved:') print(msolver.node.Es[:2]) print('Runtime: {0:.3f} (s)'.format(time() - t)) f = lambda x: np.round(x, 2) print(solver.node.Hx.shape) print(msolver.node.Hx.shape) Dx = solver.node.Hx - msolver.node.Hx print('Dx diffs...') for i, j in np.transpose(np.nonzero(Dx)): print('\t{0}:{1} :: {2:.3f}'.format(i, j, Dx[i, j])) # resolve for _ in range(TRIALS): t = time() nx, nz = solver.ground_fields() solver = RP_Solver(h, J, gam, nx=nx, nz=nz, verbose=False, cache_dir=cache_dir) solver.solve() print('\n' * 3) print('New RP solution:') print(solver.node.Es[:2]) print('Runtime: {0:.3f} (s)'.format(time() - t))