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 classify_key_list(key_list, ref_key): key_list = sort_key_list(key_list) keys_S = [] keys_D = [] keys_T = [] keys_Q = [] keys_P = [] keys_H = [] for key in key_list: if key != ref_key: orbs_i, sign, orbs_diff, count = key_ops.difference(ref_key, key) if count == 1: keys_S.append(key) elif count == 2: keys_D.append(key) elif count == 3: keys_T.append(key) elif count == 4: keys_Q.append(key) elif count == 5: keys_P.append(key) elif count == 6: keys_H.append(key) key_list = [ref_key] + keys_S + keys_D + keys_T + keys_Q + keys_P + keys_H return key_list
def mat_element(key_i, key_j, ref_energy = 0): orbs_i, sign, orbs_diff, count = key_ops.difference(key_i, key_j) if orbs_i != None: entry = sandwich(orbs_i, orbs_diff) if abs(entry) < 1e-8: entry = 0 if key_i == key_j: entry -= ref_energy if not sign: entry = -entry else: entry = 0 return entry
def gen_matrix(): """Creates a MATLAB file of the full Hamiltonian as a sparse matrix.""" dets_p = {} dets_c = {} spatial_orbs_list = [] key_list = [] for i in range(0, 8): for j in range(i+1, 8): for k in range(j+1, 8): spatial_orbs_list.append([k, j, i]) for sp_orbs_i in spatial_orbs_list: for sp_orbs_j in spatial_orbs_list: sp_orbs_temp = sp_orbs_j[:] for k in range(3): sp_orbs_temp[k] += 8 orbs = tuple(sp_orbs_temp + sp_orbs_i) key = key_ops.orbs_2_key(orbs) key_list.append(key) for key in key_list: dets_c[key] = det.Det(1, True) det_ops.merge(dets_p, dets_c) f = open('matrix_m8n6.m', '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 = black_box.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() return dets_p
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 corr_by_proj(dets_p, ref_key): """Calculates correlation energy by projection.""" # E_corr = sum_j(<D_j|H|D_0>*(<N_j>/<N_0>)) - E_0 ref_orbs = key_ops.key_2_orbs(ref_key) numer = 0 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 = black_box.sandwich(orbs_gnd, orbs_diff) * dets_p[key].value if not sign_exc: term = -term numer += term denom = float(dets_p[ref_key].value) return numer, denom