def write_mat_2_file(file_name = 'Hamiltonian_N2.m'): """Writes the full Hamiltonian as well as some MATLAB codes into a .m file.""" key_list = gen_key_list() f = open(file_name, 'w') f.write('data = [') for i in range(len(key_list)): key_i = key_list[i] for j in range(len(key_list)): key_j = key_list[j] orbs_i, sign, orbs_diff = key_ops.difference(key_i, key_j) if orbs_i != None: entry = integrals.sandwich(orbs_i, orbs_diff) if entry == 0: continue elif not sign: entry = -entry f.write(str(i+1)) f.write(' ') f.write(str(j+1)) f.write(' ') f.write('{0:.4f}'.format(entry)) f.write(' ') f.write('];\r\ndata = reshape(data, 3, length(data)/3);\r\ni = data(1,:);\r\nj = data(2,:);\r\n') f.write('s = data(3,:);\r\nH = sparse(i,j,s);\r\neig_vals = eigs(H);\r\ngnd = min(eig_vals);') f.close()
def spawn(dets_p, dets_c, key_p, spawn_map, tau, update_spawn_map_flag): """Spawning of all parents on the determinant indexed by key_p.""" # Spawning. p_sign = (dets_p[key_p].value > 0) p_u_num = abs(dets_p[key_p].value) count = 0 for count in range(p_u_num): # Single or double excitation. rand_val = random.random() if rand_val < ctrl_panel.single_prob: # Single excitation. orbs_p, key_c, sign, p_gen, orbs_diff = key_ops.single_excite(key_p) p_sing_or_doub = ctrl_panel.single_prob else: # Double excitation. orbs_p, key_c, sign, p_gen, orbs_diff = key_ops.double_excite(key_p) p_sing_or_doub = 1 - ctrl_panel.single_prob mat_element = integrals.sandwich(orbs_p, orbs_diff) if not sign: # Accounts for a possible negative sign from permuting the spawned determinant. mat_element = -mat_element prob = tau * mat_element / p_gen / p_sing_or_doub # Gets the sign of children. if prob < 0: c_sign = p_sign prob = -prob else: c_sign = not p_sign c_num = int(prob) # At least we have c_num children. prob_frac = prob - c_num rand_val = random.random() if rand_val < prob_frac: # One more child. c_num += 1 if c_num != 0: # Add c_num children to the list. if not c_sign: c_num = -c_num if key_c in dets_c: dets_c[key_c].value += c_num if dets_c[key_c].value == 0: del dets_c[key_c] else: dets_c[key_c].flag = dets_p[key_p].flag else: dets_c[key_c] = det.Det(c_num, dets_p[key_p].flag) if update_spawn_map_flag: key_index_p = ctrl_panel.key_list.index(key_p) spawn_map[key_index_p][ctrl_panel.key_list.index(key_c)] += abs(c_num)
def corr_by_proj(dets_p, ref_key): """Calculates correlation energy by projection.""" # E_corr = sum_j!=o(<D_j|H|D_0>*(<N_j>/<N_0>)) # Returns numerator and denominator separately, such that they can both be averaged over iterations. ref_orbs = key_ops.key_2_orbs(ref_key) numer = 0 # Initializes numerator. for key in dets_p: if key != ref_key: orbs_gnd, sign_exc, orbs_diff = key_ops.difference(ref_key, key) if orbs_gnd != None: term = integrals.sandwich(orbs_gnd, orbs_diff) * dets_p[key].value if not sign_exc: term = -term numer += term denom = float(dets_p[ref_key].value) # Denominator. return numer, denom
def set_diag_entry(self, key): """Sets diagonal matrix element of this determinant.""" # Excludes the reference energy of D0. orbs = key_ops.key_2_orbs(key) self.diag_entry = integrals.sandwich(orbs, ()) - integrals.ref_energy
change_shift_crit_num = 5000 init_crit_w_num = 10 init_walker_num = 50 max_iter_num = 15000 wait_for_aver_num = 2500 damping = 0.05 init_shift = 0.05 ref_key = (1, 15, 1, 15) single_prob = 0.5 tau = 0.0025 change_shift_step = 5 update_plots_step = 20 # The exact correlation energy is found by directly diagonalizing the Hamiltonian on MATLAB. exact_corr = -0.1543290876 # The ranges of various plots. orb_num = integrals.para_list[2] e_num = integrals.para_list[3] y_axis_eig_vec_plot = [-1, 1] y_axis_energy_plot = [-0.3, 0.1] y_axis_log_w_num_plot = [3, 11] # Sets the reference energy. integrals.ref_energy = integrals.sandwich(key_ops.key_2_orbs(ref_key), ()) # Begins the simulation. import run_simu aver_shift, aver_proj, dets_p, vec = run_simu.run()