Esempio n. 1
0
def test_kin():
    coord, atom = read_xyz.easy_read("test_coord.xyz", None, False, False)
    coord = coord[0,:,:]
    coord = read_xyz.xyz_to_bohr(coord)
    zoa = read_xyz.atom_to_zoa(atom)
    noa = atom.shape[0]
    noo, orb_coord, orb_type, coef_mat, exp_mat = create_basis.basisset(noa, zoa, coord)
    kin_mat = eval_kin.get_kin_mat(noo, orb_coord, orb_type, coef_mat, exp_mat)
    kin_ref =  np.array([[29.00324,  -0.16801,   0.00000,   0.00000,   0.00000,  -0.00252,  -0.00252],
    [-0.16801,   0.80813,   0.00000,   0.00000,   0.00000,   0.12857,   0.12857],
    [ 0.00000,  0.00000,   2.52873,   0.00000,  0.00000,   0.22476,  -0.22476],
    [ 0.00000,   0.00000,   0.00000,   2.52873,   0.00000,   0.17396,   0.17396],
    [ 0.00000,   0.00000,   0.00000,   0.00000,   2.52873,   0.00000,   0.00000],
    [-0.00252,   0.12857,   0.22476,   0.17396,   0.00000,   0.76003,   0.00848],
    [ -0.00252,   0.12857,  -0.22476,   0.17396,   0.00000,   0.00848,   0.76003]])
    #assert kin_mat == pytest.approx(kin_ref.astype('float64'), rel = 0.01)
    #np.set_printoptions(precision=4, suppress=True)
    #print(kin_mat-kin_ref)
    #print()
    #print(kin_mat)
    #print()
    #print(kin_ref)
    #print()
    #print((kin_mat-kin_ref)/kin_mat)
    #print()
    assert kin_mat == pytest.approx(kin_ref.astype('float64') , rel=1e-3, abs=1e-4)
Esempio n. 2
0
def test_orca_water():
    coord, atom = read_xyz.easy_read("test_coord.xyz", None, False, False)
    coord = coord[0, :, :]
    coord = read_xyz.xyz_to_bohr(coord)
    zoa = read_xyz.atom_to_zoa(atom)
    noa = atom.shape[0]
    noo, orb_coord, orb_type, coef_mat, exp_mat = create_basis.basisset(
        noa, zoa, coord)
    read_xyz.plot_initial_orbs(noo, orb_coord, orb_type, coef_mat, exp_mat)

    over_mat = eval_over.get_overlap_mat(noo, orb_coord, orb_type, coef_mat,
                                         exp_mat)
    np.set_printoptions(precision=4, suppress=True)
    kin_mat = eval_kin.get_kin_mat(noo, orb_coord, orb_type, coef_mat, exp_mat)
    nuc_mat = eval_nuc.get_nuc_mat(noo, orb_coord, orb_type, coef_mat, exp_mat,
                                   coord, zoa)

    dip_matx = eval_dipole.get_dip_mat(noo, orb_coord, orb_type, coef_mat,
                                       exp_mat, 0)
    dip_maty = eval_dipole.get_dip_mat(noo, orb_coord, orb_type, coef_mat,
                                       exp_mat, 1)
    dip_matz = eval_dipole.get_dip_mat(noo, orb_coord, orb_type, coef_mat,
                                       exp_mat, 2)
    dip_mat = np.array([dip_matx, dip_maty, dip_matz])

    four_mat = eval_four.get_four_mat(noo, orb_coord, orb_type, coef_mat,
                                      exp_mat)
    h = kin_mat + nuc_mat
    ##diagonalisation of S
    #eig, u = np.linalg.eig(over_mat)
    ##creation of matrix x via canonical orthogonalisation
    #x = u / (eig) ** 0.5
    ## xt is transposed of x
    #xt = x.T
    x = scf.ortho_basis(over_mat)
    noe = zoa.sum()
    #print("number of electron:", noe)
    #en, p, c = scf.scf(four_mat, noe, noo, x, xt, h)
    en, p, c, coeff = scf.scf(four_mat, noe, noo, x, h)
    etot = scf.calc_tot_en(noa, en, coord, zoa)
    #print(etot)
    #dipole = dip_mat * p
    #dipole = dipole.sum(axis=1)
    #dipole = dipole.sum(axis=1)
    dipole = eval_dipole.get_el_dipole(p, dip_mat)
    nuc_pole = eval_dipole.get_nuc_pole(coord, zoa)
    tot_dip = nuc_pole - dipole
    orca_en = -74.962919685003
    orca_dip = np.array([0.000000, 0.678999, -0.000000])
    assert etot == pytest.approx(orca_en, rel=1e-5, abs=1e-4)
    assert tot_dip == pytest.approx(orca_dip, rel=1e-5, abs=1e-3)
Esempio n. 3
0
def test_over():
    coord, atom = read_xyz.easy_read("test_coord.xyz", None, False, False)
    coord = coord[0,:,:]
    coord = read_xyz.xyz_to_bohr(coord)
    zoa = read_xyz.atom_to_zoa(atom)
    noa = atom.shape[0]
    noo, orb_coord, orb_type, coef_mat, exp_mat = create_basis.basisset(noa, zoa, coord)
    #read_xyz.plot_initial_orbs(noo, orb_coord, orb_type, coef_mat, exp_mat)
    over_mat = eval_over.get_overlap_mat(noo, orb_coord, orb_type, coef_mat, exp_mat)
    over_ref =  np.array([[1.00000,   0.23670,   0.00000,   0.00000,   0.00000,   0.05397,   0.05397],
    [0.23670,   1.00000,   0.00000,   0.00000,   0.00000,   0.47475,   0.47475],
    [0.00000,   0.00000,   1.00000,   0.00000,   0.00000,   0.31112,  -0.31112],
    [ 0.00000,   0.00000,   0.00000,  1.00000,   0.00000,   0.24081,   0.24081],
    [ 0.00000,   0.00000,   0.00000,   0.00000,   1.00000,   0.00000,   0.00000],
    [ 0.05397,   0.47475,   0.31112,   0.24081,   0.00000,   1.00000,   0.25167],
    [ 0.05397,   0.47475,  -0.31112,   0.24081,   0.00000,   0.25167,   1.00000]])
    assert over_mat == pytest.approx(over_ref.astype('float64'), rel = 0.001)
Esempio n. 4
0
def test_create_basis():
    coord, atom = read_xyz.easy_read("test_coord.xyz", None, False, False)
    coord = coord[0, :, :]
    zoa = read_xyz.atom_to_zoa(atom)
    noa = atom.shape[0]
    #basisset(noa, zoa, coord)
    noo, orb_coord, orb_type, coef_mat, exp_mat = create_basis.basisset(
        noa, zoa, coord)

    ref_exp = np.array([[6.44364, 23.80886, 130.70929],
                        [0.38039, 1.16959,
                         5.03315], [0.38039, 1.16959, 5.03315],
                        [0.38039, 1.16959,
                         5.03315], [0.38039, 1.16959, 5.03315],
                        [0.16886, 0.62391, 3.42525],
                        [0.16886, 0.62391, 3.42525]])
    #print(ref_exp[:,::-1])
    #assert ref_exp[:,::-1].astype('float64') == pytest.approx(coef_mat)
    assert ref_exp[:, ::-1] == pytest.approx(exp_mat.astype('float64'),
                                             rel=0.0001)
Esempio n. 5
0
def test_easy_read():
    ref_coord = np.array([[0.00000, 0.00000, 0.0000],
                          [0.75690, 0.58585, 0.0000],
                          [-0.75690, 0.58585, 0.0000]])
    ref_atom = np.array(["O", "H", "H"])
    ref_z = np.array([8, 1, 1])
    coord, atom = read_xyz.easy_read("test_coord.xyz", None, False, False)
    coord = coord[0, :, :]
    zoa = read_xyz.atom_to_zoa(atom)
    assert coord == pytest.approx(ref_coord)
    #print(ref_atom == atom)
    #assert
    assert (atom == ref_atom).all()
    #assert len(actual) == len(expected)
    #assert all([a == b for a, b in zip(actual, expected)])
    #assert atom.all()  == ref_atom.all()
    assert (ref_z == zoa).all()
    #assert coord == pytest.approx(ref_coord)
    bohr_in_a = 0.529177210903
    coord = read_xyz.xyz_to_bohr(coord)
    ref_coord /= bohr_in_a
    assert coord == pytest.approx(ref_coord)
Esempio n. 6
0
def test_nuc():
    coord, atom = read_xyz.easy_read("test_coord.xyz", None, False, False)
    coord = coord[0,:,:]
    coord = read_xyz.xyz_to_bohr(coord)
    zoa = read_xyz.atom_to_zoa(atom)
    noa = atom.shape[0]
    noo, orb_coord, orb_type, coef_mat, exp_mat = create_basis.basisset(noa, zoa, coord)
#NUCLEAR-ATTRACTION MATRIX
    nuc_mat = eval_nuc.get_nuc_mat(noo, orb_coord, orb_type, coef_mat, exp_mat, coord, zoa)
    #print("nuclear attraction  matrix: ")
    nuc_ref = np.array([[-61.72413,  -7.44477,   0.00000,  -0.01900,   0.00000,  -1.74672,  -1.74672],
 [-7.44477, -10.14287,   0.00000,  -0.22350,   0.00000,  -3.86944,  -3.86944],
 [ 0.00000,   0.00000, -10.14262,   0.00000,   0.00000,  -2.25477,   2.25477],
 [-0.01900,  -0.22350,   0.00000, -10.07996,   0.00000,  -1.81819,  -1.81819],
 [ 0.00000,   0.00000,   0.00000,   0.00000,  -9.98632,   0.00000,   0.00000],
 [-1.74672,  -3.86944,  -2.25477,  -1.81819,   0.00000,  -5.83642,  -1.61650],
 [-1.74672,  -3.86944,   2.25477,  -1.81819,   0.00000,  -1.61650,  -5.83642]])
    
    np.set_printoptions(precision=4, suppress=True)
    print(nuc_ref - nuc_mat)
    print(nuc_mat)
    assert nuc_mat == pytest.approx(nuc_ref.astype('float64'), rel = 0.001)
Esempio n. 7
0
def test_four_ss():
    coord, atom = read_xyz.easy_read("test_coord.xyz", None, False, False)
    coord = coord[0,:,:]
    coord = read_xyz.xyz_to_bohr(coord)
    zoa = read_xyz.atom_to_zoa(atom)
    noa = atom.shape[0]
    noo, orb_coord, orb_type, coef_mat, exp_mat = create_basis.basisset(noa, zoa, coord)
    four_mat = eval_four.get_four_mat(noo, orb_coord, orb_type, coef_mat, exp_mat)
    assert four_mat[0,0,0,0] == pytest.approx(4.78508, rel = 0.001) #ssss
    assert four_mat[0,1,0,0] == pytest.approx(0.74138, rel = 0.001) #ssss
    assert four_mat[5,5,5,3] == pytest.approx(0.19847, rel = 0.001) #psss
    assert four_mat[6,6,6,3] == pytest.approx(0.19847, rel = 0.001) #psss
    assert four_mat[6,6,6,2] == pytest.approx(-0.25641, rel = 0.001)#psss
#0.19847
#7 7 5 5     0.48275
    assert four_mat[6,6,4,4] == pytest.approx(0.48275, rel = 0.001)#ppss
    assert four_mat[6,6,4,3] == pytest.approx(0.0, rel = 0.001)#ppss
#7 5 7 5     0.02343
    assert four_mat[6,4,6,4] == pytest.approx(0.02343, rel = 0.001)#psps
#test_nuc()
#7 4 3 3     0.15098
##7 4 4 1     0.01162
##7 4 4 2     0.07176
#7 4 4 3    -0.01229
    assert four_mat[6,3,2,2] == pytest.approx(0.15098, rel = 0.001)#ppps
    assert four_mat[6,3,3,2] == pytest.approx(-0.01229, rel = 0.001)#ppps
#4 4 4 4     0.88016
    assert four_mat[3,3,3,3] == pytest.approx(0.88016, rel = 0.001)#ppps
#def test_four_full():
    four_pre = np.loadtxt("../data/FOUR_CENTRE_TEST_DATA.dat")
    for i in range(four_pre.shape[0]):
#    for i in range(3):
#        print("hallllllllllllllllllllloooooooooooooooooooo")
        print(i)
        #assert four_mat[int(four_pre[i,0])-1,int(four_pre[i,1])-1,int(four_pre[i,2])-1,int(four_pre[i,3])-1] == pytest.approx(four_pre[i,4], rel = 0.05)
        assert four_mat[int(four_pre[i,0])-1,int(four_pre[i,1])-1,int(four_pre[i,2])-1,int(four_pre[i,3])-1] == pytest.approx(four_pre[i,4], rel=1e-3, abs=1e-5)
Esempio n. 8
0
def test_form_g_mat():
    #dens_xxx_01  fock_xxx_01 coord_xxx_01.xyz
    orca_dens = np.loadtxt("../data/dens_xxx_01")
    orca_fock = np.loadtxt("../data/fock_xxx_01")
    orca_h = np.loadtxt("../data/h_xxx_01")
    orca_fock2 = np.zeros(orca_fock.shape)
    orca_dens2 = np.zeros(orca_dens.shape)
    orca_h2 = np.zeros(orca_h.shape)

    dict1 = {}
    dict1[0] = 0
    dict1[1] = 1
    dict1[2] = 4
    dict1[3] = 2
    dict1[4] = 3
    dict1[5] = 5
    dict1[6] = 6

    for i in range(orca_fock2.shape[0]):
        for j in range(orca_fock2.shape[1]):
            #k = i
            #l = j
            #if i  =
            orca_fock2[dict1[i], dict1[j]] = orca_fock[i, j]
            orca_dens2[dict1[i], dict1[j]] = orca_dens[i, j]
            orca_h2[dict1[i], dict1[j]] = orca_h[i, j]
    p = orca_dens2
    #p =np.array([[2.10625, -0.44603, 0.00000, 0.10859, 0.00000, -0.02843, -0.02843],
    # [-0.44603, 1.96730, 0.00000,-0.61771, 0.00000, -0.03406, -0.03406],
    # [0.00000,  0.00000, 0.73554, 0.00000, 0.00000,  0.53974, -0.53974],
    # [0.10859, -0.61771, 0.00000, 1.24023, 0.00000,  0.47272,  0.47272],
    # [0.00000,  0.00000, 0.00000, 0.00000, 2.00000,  0.00000,  0.00000],
    # [-0.02843,-0.03406, 0.53974, 0.47272, 0.00000,  0.60093, -0.19120],
    # [-0.02843,-0.03406,-0.53974, 0.47272, 0.00000, -0.19120,  0.60093]])
    coord, atom = read_xyz.easy_read("../data/coord_xxx_01.xyz", None, False,
                                     False)
    coord = coord[0, :, :]
    coord = read_xyz.xyz_to_bohr(coord)
    zoa = read_xyz.atom_to_zoa(atom)
    noa = atom.shape[0]
    #basisset(noa, zoa, coord)
    noo, orb_coord, orb_type, coef_mat, exp_mat = create_basis.basisset(
        noa, zoa, coord)
    #print(noo)
    #print(orb_coord)
    #print(orb_type)
    #print(coef_mat)
    #print(exp_mat)
    read_xyz.plot_initial_orbs(noo, orb_coord, orb_type, coef_mat, exp_mat)
    #    print(coef_mat.dtype)

    over_mat = eval_over.get_overlap_mat(noo, orb_coord, orb_type, coef_mat,
                                         exp_mat)
    np.set_printoptions(precision=5, suppress=True)
    kin_mat = eval_kin.get_kin_mat(noo, orb_coord, orb_type, coef_mat, exp_mat)
    nuc_mat = eval_nuc.get_nuc_mat(noo, orb_coord, orb_type, coef_mat, exp_mat,
                                   coord, zoa)

    print("pre_noo:", noo)
    four_mat = eval_four.get_four_mat(noo, orb_coord, orb_type, coef_mat,
                                      exp_mat)

    h = kin_mat + nuc_mat
    g = scf.formg(noo, four_mat, p)
    f = h + g
    #fock_ref = np.loadtxt("../data/fock.data")
    #print()
    #print(f - fock_ref)
    #print()
    #print((f - fock_ref)/fock_ref)
    #print()
    #print(f )
    #print()
    #print(fock_ref)

    #../data/orca_dens_eq ../data/orca_fock_eq
    #assert f == pytest.approx(fock_ref, rel=1e-3, abs=1e-5)

    print(f - orca_fock2)
    print()
    #print((f - orca_fock2)/f)
    print()
    print(f)
    print()
    print(orca_fock2)

    assert h == pytest.approx(orca_h2, rel=1e-3, abs=1e-4)
    assert f == pytest.approx(orca_fock2, rel=1e-3, abs=1e-4)
    assert p == pytest.approx(orca_dens2, rel=1e-3, abs=1e-4)
Esempio n. 9
0
def main():
    print("sonntag")
    print(
        "Performing a  self-consistent HF calculation of the molecule given by the file  coord.xyz in this directory ... "
    )
    print("Reading coordinates ... ")
    coord, atom = read_xyz.easy_read("coord.xyz", None, False, False)
    coord = coord[0, :, :]
    coord = read_xyz.xyz_to_bohr(coord)
    zoa = read_xyz.atom_to_zoa(atom)
    noa = atom.shape[0]
    noo, orb_coord, orb_type, coef_mat, exp_mat = create_basis.basisset(
        noa, zoa, coord)
    read_xyz.plot_initial_orbs(noo, orb_coord, orb_type, coef_mat, exp_mat)

    over_mat = eval_over.get_overlap_mat(noo, orb_coord, orb_type, coef_mat,
                                         exp_mat)
    print("overlap matrix: ")
    np.set_printoptions(precision=4, suppress=True)
    print(over_mat.astype('float64'))

    kin_mat = eval_kin.get_kin_mat(noo, orb_coord, orb_type, coef_mat, exp_mat)
    print("kinetic energy  matrix: ")
    print(kin_mat.astype('float64'))

    nuc_mat = eval_nuc.get_nuc_mat(noo, orb_coord, orb_type, coef_mat, exp_mat,
                                   coord, zoa)
    print("nuclear attraction  matrix: ")
    print(nuc_mat.astype('float64'))

    dip_matx = eval_dipole.get_dip_mat(noo, orb_coord, orb_type, coef_mat,
                                       exp_mat, 0)
    dip_maty = eval_dipole.get_dip_mat(noo, orb_coord, orb_type, coef_mat,
                                       exp_mat, 1)
    dip_matz = eval_dipole.get_dip_mat(noo, orb_coord, orb_type, coef_mat,
                                       exp_mat, 2)
    dip_mat = np.array([dip_matx, dip_maty, dip_matz])
    print("dipole moment matrix: ")
    print(dip_mat.astype('float64'))

    print("calculate four center  integrals.... ")
    four_mat = eval_four.get_four_mat(noo, orb_coord, orb_type, coef_mat,
                                      exp_mat)
    #print(four_mat[0,0,0,0])
    #print(four_mat[5,5,5,3])

    #create core hamiltonian
    h = kin_mat + nuc_mat

    x = scf.ortho_basis(over_mat)
    noe = zoa.sum()
    print("number of electron:", noe)
    en, p, coeff, energy_orb = scf.scf(four_mat, noe, noo, x, h)
    print()
    print("#######################################################")
    print("final orbital energies:", energy_orb)
    print("#######################################################")
    #print()
    print()
    print("#######################################################")
    print("final electronic energy:", en)
    print("#######################################################")
    print()

    etot = scf.calc_tot_en(noa, en, coord, zoa)
    print("#######################################################")
    print("final total energy:", etot)
    print("#######################################################")
    #print(etot)
    dipole = eval_dipole.get_el_dipole(p, dip_mat)
    print()
    print()
    print()
    print("#######################################################")
    print("electronic contribution dipole moment:", dipole)
    nuc_pole = eval_dipole.get_nuc_pole(coord, zoa)
    print("nucear contribution dipole moment:", nuc_pole)
    print("total dipole moment:", nuc_pole - dipole,
          np.linalg.norm(nuc_pole - dipole))
    print("#######################################################")

    np.savetxt("coefficients", coeff[:int(noe / 2), :])
    np.savetxt("density_mat", p)
Esempio n. 10
0
def dens_to_cube():
    print("command line:")
    print(sys.argv)
    fn_dens = str(sys.argv[1])
    np.set_printoptions(precision=4, suppress=True)
    dens = np.loadtxt(fn_dens)
    coord, atom = read_xyz.easy_read("coord.xyz", None, False, False)
    coord = coord[0, :, :]
    coord = read_xyz.xyz_to_bohr(coord)
    zoa = read_xyz.atom_to_zoa(atom)
    noa = atom.shape[0]
    noo, orb_coord, orb_type, coef_mat, exp_mat = create_basis.basisset(
        noa, zoa, coord)
    n_grid = 140
    cube_length_au = 7 / bohr_in_a
    data = np.zeros((n_grid, n_grid, n_grid))
    mesh = data.shape
    origin_au = np.array([
        cube_length_au / 2, cube_length_au / 2, cube_length_au / 2
    ])  #punkt in rechter!!!!! unterer ecke, nicht die box mitte!!!
    cell_au = np.eye(3) * cube_length_au / n_grid
    grid = CubeFileTools.CalcGridPositions(cell_au, mesh, origin_au)
    comment1 = " "
    comment2 = " "
    #numbers = zoa

    cell_data = dict()
    cell_data['numbers'] = zoa
    #cell_data['coords_au'] = coords_au
    cell_data['coords_au'] = coord
    cell_data['cell_au'] = cell_au
    #cell_data['symbols'  ] = [constants.symbols[int(e)-1] for e in cell_data['numbers']]
    cell_data['symbols'] = atom
    cell_data['species'] = list(set(cell_data['symbols']))
    cell_data['mesh'] = data.shape
    cell_data['d3r_au'] = cell_au[0, 0] * cell_au[1, 1] * cell_au[2, 2]
    cell_data['volume_au'] = cell_data['d3r_au'] * data.shape[0] * data.shape[
        1] * data.shape[2]
    cell_data['r_au'] = grid
    #cell_data['r_au'     ] = CalcGridPositions(cell_au, data.shape, origin_au=origin_au)
    cell_data['data'] = data
    cell_data['comment1'] = comment1
    cell_data['comment2'] = comment2
    cell_data['origin_au'] = -origin_au
    #species  symbols  numbers  comment1  comment2
    #def WriteCubeFile(filename, comment1, comment2, numbers, coords, cell, data, origin=np.zeros(3)):
    #fn_out = "test.cube"
    fn_out = fn_dens + ".cube"

    print("start to calc density on grid")
    basis_at_grid = []
    for i in range(noo):
        #print("input ",orb_type[i], exp_mat[i], coef_mat[i], orb_coord[i])
        data = basis_func_to_grid(grid, orb_type[i], exp_mat[i], coef_mat[i],
                                  orb_coord[i])
        #str1 = "basis_function"+str(i)+".cube"
        #cube.WriteCubeFile(str1, cell_data['comment1' ], cell_data['comment2' ], cell_data['numbers'  ], cell_data['coords_au'], cell_data['cell_au'  ], data, cell_data['origin_au'])
        basis_at_grid.append(data)
    basis_at_grid = np.array(basis_at_grid)
    basis_overlapp_states = []
    for i in range(noo):
        basis_overlapp_states.append([])
        for j in range(noo):
            basis_overlapp_states[i].append(basis_at_grid[i] *
                                            basis_at_grid[j])
    basis_overlapp_states = np.array(basis_overlapp_states)
    #print(basis_overlapp_states.shape)
    #strange_dens_mat = basis_overlapp_states.sum(axis=(2,3,4))
    #print(strange_dens_mat)
    #basis_overlapp_states = basis_overlapp_states*dens
    basis_overlapp_states1 = basis_overlapp_states * dens[:, :, np.newaxis,
                                                          np.newaxis,
                                                          np.newaxis]
    data = basis_overlapp_states1.sum(axis=(0, 1))
    #fn_out =
    cube.WriteCubeFile(fn_out, cell_data['comment1'], cell_data['comment2'],
                       cell_data['numbers'], cell_data['coords_au'],
                       cell_data['cell_au'], data, cell_data['origin_au'])
Esempio n. 11
0
def calc_moments_numerically():
    #fn1 = "../data/dens_h2o.cube"
    #fn1 = "/home/dressler/projects/HF/develop_bombanitio/minus9/data/dens_h2o.cube"
    #fn1 = "/home/dressler/projects/HF/develop_bombanitio/minus9/data/DENSITY.cube"
    #cube1 = CubeFileTools.LoadCellData(fn1)
    #for i in cube1.keys():
    #    print(i)
    #print(cube1["origin_au"])
    ##print(cube1["cell_au"])
    #print(cube1["data"].shape)
    #print(cube1["coords_au"])
    np.set_printoptions(precision=4, suppress=True)
    print("Reading coordinates ... ")
    fn_dens = "density_mat0"
    #dens = np.loadtxt("density_mat0")
    dens = np.loadtxt(fn_dens)
    coord, atom = read_xyz.easy_read("coord.xyz", None, False, False)
    coord = coord[0, :, :]
    coord = read_xyz.xyz_to_bohr(coord)
    zoa = read_xyz.atom_to_zoa(atom)
    noa = atom.shape[0]
    noo, orb_coord, orb_type, coef_mat, exp_mat = create_basis.basisset(
        noa, zoa, coord)
    #read_xyz.plot_initial_orbs(noo, orb_coord, orb_type, coef_mat, exp_mat)

    #n_grid =  108
    #n_grid =  216
    n_grid = 140
    cube_length_au = 7 / bohr_in_a
    #cube_length_au =  10 / bohr_in_a
    #data = np.zeros((108,108,108))
    data = np.zeros((n_grid, n_grid, n_grid))
    mesh = data.shape
    #origin_au = [-5,-5,-5]/bohr_in_a #punkt in linker unterer ecke, nicht die box mitte!!!
    origin_au = np.array([
        cube_length_au / 2, cube_length_au / 2, cube_length_au / 2
    ])  #punkt in rechter!!!!! unterer ecke, nicht die box mitte!!!
    #origin_au = np.array([cube_length_au ,cube_length_au,cube_length_au]) #punkt in rechter!!!!! unterer ecke, nicht die box mitte!!!
    cell_au = np.eye(3) * cube_length_au / n_grid
    #def CalcGridPositions(cell_au, mesh, origin_au=None):
    grid = CubeFileTools.CalcGridPositions(cell_au, mesh, origin_au)

    comment1 = "bla"
    comment2 = "blub"
    #numbers = zoa

    cell_data = dict()
    cell_data['numbers'] = zoa
    #cell_data['coords_au'] = coords_au
    cell_data['coords_au'] = coord
    cell_data['cell_au'] = cell_au
    #cell_data['symbols'  ] = [constants.symbols[int(e)-1] for e in cell_data['numbers']]
    cell_data['symbols'] = atom
    cell_data['species'] = list(set(cell_data['symbols']))
    cell_data['mesh'] = data.shape
    cell_data['d3r_au'] = cell_au[0, 0] * cell_au[1, 1] * cell_au[2, 2]
    cell_data['volume_au'] = cell_data['d3r_au'] * data.shape[0] * data.shape[
        1] * data.shape[2]
    cell_data['r_au'] = grid
    #cell_data['r_au'     ] = CalcGridPositions(cell_au, data.shape, origin_au=origin_au)
    cell_data['data'] = data
    cell_data['comment1'] = comment1
    cell_data['comment2'] = comment2
    cell_data['origin_au'] = -origin_au
    #species  symbols  numbers  comment1  comment2
    #def WriteCubeFile(filename, comment1, comment2, numbers, coords, cell, data, origin=np.zeros(3)):
    fn_out = "test.cube"
    #cube.WriteCubeFile(fn_out, cell_data['comment1' ], cell_data['comment2' ], cell_data['numbers'  ], cell_data['coords_au'], cell_data['cell_au'  ], cell_data['data'     ], cell_data['origin_au'])

    mompol = dict()
    moments = dict()

    exp_order = 1
    n_states = 3
    for i_order in range(1, exp_order + 1):
        #f cartesian:
        #   mompol['%02d'%i_order] = CubeFileTools.CartesianMoments(r_au, order=i_order)
        #lse:
        #mompol['%02d'%i_order] = CubeFileTools.CartesianMoments(r_au, order=i_order)
        mompol['%02d' % i_order] = CubeFileTools.CartesianMoments(
            grid, order=i_order)
        #mompol['%02d'%i_order] = SphericalHarmonics.RealRegularSolidHarmonics(r_au, order=i_order)
#moments['%02d'%i_order] = np.zeros((n_states, mompol['%02d'%i_order].shape[0]))
#print(mompol)
#print(moments)
#mom_pol_ar = np.zeros((n_states, 108, 108 , 108))
#mom_pol_ar = np.zeros((n_states + 1 , 108, 108 , 108))
#mom_pol_ar = np.zeros((n_states  , 108, 108 , 108))
    mom_pol_ar = np.zeros((n_states, n_grid, n_grid, n_grid))
    #for i in range(n_states):
    #mom_pol_ar[0] = np.ones((108, 108 , 108))

    print(n_states)

    for j in range(n_states):
        #k = j +1
        k = j
        if (j < 3) and (j > -1):
            mom_pol_ar[k] = mompol['01'][j]
        elif (j < 9) and (j > 2):
            #            ipdb.set_trace()
            mom_pol_ar[k] = mompol['02'][j - 3]
        elif (j < 19) and (j > 8):
            mom_pol_ar[k] = mompol['03'][j - 9]
        elif (j < 34) and (j > 18):
            mom_pol_ar[k] = mompol['04'][j - 19]
        elif (j < 55) and (j > 33):
            mom_pol_ar[k] = mompol['05'][j - 34]
        else:
            print('index error')


#mom_pol_ar.shape

#if 5 > 10:
#lime.print_states_dict(mom_pol_ar*0.001, cell_data5, n_states, '/shared/dressler/chi/cartesian-storage-fac-0.001/cartesian-functions-%05d', pure = True, pert = True)
#lime.print_states_dict(mom_pol_ar*0.001, cell_data5, n_states + 1, '/shared/dressler/chi/paper_no4_theorem/data/v1/center-chi4-basis-polynoms-shift/cartesian-functions-%05d', pure = True, pert = True)
#lime. print_states_dict(mom_pol_ar*0.001, cell_data, n_states + 1, 'cartesian-functions-%05d', pure = True, pert = True)
#lime.print_states_dict(mom_pol_ar*0.001, cell_data, n_states, 'cartesian-functions-%05d', pure = True, pert = True)
    for i in range(n_states):
        fnx = "cartesian-functions" + str(i) + ".cube"
        cube.WriteCubeFile(fnx, cell_data['comment1'], cell_data['comment2'],
                           cell_data['numbers'], cell_data['coords_au'],
                           cell_data['cell_au'], mom_pol_ar[i],
                           cell_data['origin_au'])

    print("start to calc density on grid")
    #print(grid.shape)
    #dist1 =  np.linalg.norm(grid, axis=0)
    #print(dist1.shape)
    ##data = np.exp(np.
    #exp1 = np.exp(dist1)
    #print(exp1.shape)
    basis_at_grid = []
    for i in range(noo):
        #noo, orb_coord, orb_type, coef_mat, exp_mat
        #data = basis_func_to_grid(grid, orb_type ,exp, coeff, pos)
        #if orb_type[i] > 0:
        print("input ", orb_type[i], exp_mat[i], coef_mat[i], orb_coord[i])
        data = basis_func_to_grid(grid, orb_type[i], exp_mat[i], coef_mat[i],
                                  orb_coord[i])
        #print(data.shape, abs(data).sum())
        print(data.shape, abs(data**2).sum())
        print(data[52, 52, 52])
        str1 = "basis_function" + str(i) + ".cube"
        cube.WriteCubeFile(str1, cell_data['comment1'], cell_data['comment2'],
                           cell_data['numbers'], cell_data['coords_au'],
                           cell_data['cell_au'], data, cell_data['origin_au'])
        basis_at_grid.append(data)
        #fn_dens +
    #basis_func_to_grid(grid,orb_type ,exp, coeff, pos)
    basis_at_grid = np.array(basis_at_grid)
    basis_overlapp_states = []
    for i in range(noo):
        basis_overlapp_states.append([])
        for j in range(noo):
            basis_overlapp_states[i].append(basis_at_grid[i] *
                                            basis_at_grid[j])
    basis_overlapp_states = np.array(basis_overlapp_states)
    print(basis_overlapp_states.shape)
    strange_dens_mat = basis_overlapp_states.sum(axis=(2, 3, 4))
    print(strange_dens_mat)
    #basis_overlapp_states = basis_overlapp_states*dens
    diff_list = []
    for fn_dens in [
            "density_mat0", "density_mat1", "density_mat2", "density_mat3"
    ]:
        #fn_dens = "density_mat0"
        dens = np.loadtxt(fn_dens)
        basis_overlapp_states1 = basis_overlapp_states * dens[:, :, np.newaxis,
                                                              np.newaxis,
                                                              np.newaxis]
        data = basis_overlapp_states1.sum(axis=(0, 1))
        str1 = fn_dens + ".cube"
        cube.WriteCubeFile(str1, cell_data['comment1'], cell_data['comment2'],
                           cell_data['numbers'], cell_data['coords_au'],
                           cell_data['cell_au'], data, cell_data['origin_au'])
        print("summ of electrons: ", data.sum())
        if fn_dens == "density_mat0":
            ref = np.copy(data)
        else:
            diff = data - ref
            str1 = "diff_" + fn_dens + ".cube"
            cube.WriteCubeFile(str1, cell_data['comment1'],
                               cell_data['comment2'], cell_data['numbers'],
                               cell_data['coords_au'], cell_data['cell_au'],
                               diff, cell_data['origin_au'])
            print("summ of diff electrons: ", diff.sum())
            diff_list.append(diff)
    diff_list = np.array(diff_list)
    #mom_mat = lib_dme.create_overlap_mat(diff_list[1:], mom_pol_ar)
    mom_mat = lib_dme.create_overlap_mat(diff_list, mom_pol_ar)
    print("mom mat")
    print(mom_mat)
    print(abs(diff_list[0]).sum())
    print((diff_list[0] * mom_pol_ar[1]).sum())
    special = diff_list[0] * mom_pol_ar[1]
    cube.WriteCubeFile("special.cube", cell_data['comment1'],
                       cell_data['comment2'], cell_data['numbers'],
                       cell_data['coords_au'], cell_data['cell_au'], special,
                       cell_data['origin_au'])
Esempio n. 12
0
def main():
    original_stdout = sys.stdout
    print("Calculation of the first three moment expanded states")
    print()
    print("initalize system and calculating one and two electron matrices...")
    with open('detailed_output_moment_expansion.txt', 'w') as f:
        sys.stdout = f
        print("Reading coordinates ... ")
        coord, atom = read_xyz.easy_read("coord.xyz", None, False, False)
        coord = coord[0, :, :]
        coord = read_xyz.xyz_to_bohr(coord)
        zoa = read_xyz.atom_to_zoa(atom)
        noa = atom.shape[0]
        noo, orb_coord, orb_type, coef_mat, exp_mat = create_basis.basisset(
            noa, zoa, coord)
        read_xyz.plot_initial_orbs(noo, orb_coord, orb_type, coef_mat, exp_mat)

        over_mat = eval_over.get_overlap_mat(noo, orb_coord, orb_type,
                                             coef_mat, exp_mat)
        print("overlap matrix: ")
        np.set_printoptions(precision=4, suppress=True)
        print(over_mat.astype('float64'))

        kin_mat = eval_kin.get_kin_mat(noo, orb_coord, orb_type, coef_mat,
                                       exp_mat)
        print("kinetic energy  matrix: ")
        print(kin_mat.astype('float64'))

        nuc_mat = eval_nuc.get_nuc_mat(noo, orb_coord, orb_type, coef_mat,
                                       exp_mat, coord, zoa)
        print("nuclear attraction  matrix: ")
        print(nuc_mat.astype('float64'))

        dip_matx = eval_dipole.get_dip_mat(noo, orb_coord, orb_type, coef_mat,
                                           exp_mat, 0)
        dip_maty = eval_dipole.get_dip_mat(noo, orb_coord, orb_type, coef_mat,
                                           exp_mat, 1)
        dip_matz = eval_dipole.get_dip_mat(noo, orb_coord, orb_type, coef_mat,
                                           exp_mat, 2)
        dip_mat = np.array([dip_matx, dip_maty, dip_matz])
        print("dipole moment matrix: ")
        print(dip_mat.astype('float64'))

        print("calculate four center  integrals.... ")
        four_mat = eval_four.get_four_mat(noo, orb_coord, orb_type, coef_mat,
                                          exp_mat)
        #print(four_mat[0,0,0,0])
        #print(four_mat[5,5,5,3])

        #create core hamiltonian
        h = kin_mat + nuc_mat

        x = scf.ortho_basis(over_mat)
        noe = zoa.sum()
        print("number of electron:", noe)
        diff_dens_list = []
        #for i in range([0,1,2,3]):
        sys.stdout = original_stdout
        print(
            "performing single point calculation with and without external potentials"
        )
        sys.stdout = f
        for i in [0, 1, 2, 3]:
            if i == 0:
                ext = None
            else:
                ext = 0.0001 * dip_mat[i - 1, :, :]

            print()
            print()
            print()
            print()
            print()
            print("#######################################################",
                  file=f)
            print("start of new single point calculation for the " + str(i) +
                  "-th moment expanded state",
                  file=f)
            print("#######################################################")
            print()

            h = kin_mat + nuc_mat
            en, p, coeff, energy_orb = scf.scf(four_mat, noe, noo, x, h, ext)
            print()
            print("#######################################################")
            print("final orbital energies:", energy_orb)
            print("#######################################################")
            #print()
            print()
            print("#######################################################")
            print("final electronic energy:", en)
            print("#######################################################")
            print()

            etot = scf.calc_tot_en(noa, en, coord, zoa)
            print("#######################################################")
            print("final total energy:", etot)
            print("#######################################################")
            #print(etot)
            dipole = eval_dipole.get_el_dipole(p, dip_mat)
            print()
            print()
            print()
            print("#######################################################")
            print("electronic contribution dipole moment:", dipole)
            nuc_pole = eval_dipole.get_nuc_pole(coord, zoa)
            print("nucear contribution dipole moment:", nuc_pole)
            print("total dipole moment:", nuc_pole - dipole)
            print("#######################################################")

            str1 = "coefficients" + str(i)
            #np.savetxt("coefficients", coeff[:int(noe/2),:])
            np.savetxt(str1, coeff[:int(noe / 2), :])
            str4 = "density_mat" + str(i)
            #np.savetxt("coefficients", coeff[:int(noe/2),:])
            np.savetxt(str4, p)

            print()
            print("#######################################################")
            #print("coefficients matrix of  " + str(i)+"th single point")
            #print(coeff[:int(noe/2),:])
            print("density matrix obtained by  " + str(i) +
                  "-th single point calculation")
            print(p)
            print("#######################################################")
            print()

            if i == 0:
                equi_coeff = np.copy(coeff[:int(noe / 2), :])
                equi_p = np.copy(p)
            else:
                str2 = "coefficients_mes" + str(i)
                str3 = "denstiy_mes" + str(i)
                diff = coeff[:int(noe / 2), :] - equi_coeff
                diff_p = p - equi_p
                np.savetxt(str2, diff)
                np.savetxt(str3, diff_p)
                diff_dens_list.append(diff_p)
                print()
                #print("#######################################################")
                #print("coefficients matrix of equilibrium single point")
                #print(equi_coeff)
                #print("#######################################################")
                #print("coefficients matrix of" + str(i)+"th moment expanded state")
                #print(diff)
                #print("#######################################################")
                print(
                    "#######################################################")
                print(
                    "density matrix from obtained from single point calculation without applied external potentials"
                )
                print(equi_p)
                print(
                    "#######################################################")
                #print("density matrix of" + str(i)+"-th moment expanded state")
                print(
                    "density matrix of electron density difference obtained by  single point calculations with the "
                    + str(i) + "-th  and without an external potential")
                print(diff_p)
                print(
                    "#######################################################")

            print()
            print("#######################################################")
            print("end of single point calculation for the " + str(i) +
                  "-th moment expanded state")
            print("#######################################################")
            print()
            print(
                "#########################################################################################################################"
            )
            print()
    sys.stdout = original_stdout
    print(
        "post processing of response densities obtained by the external potential"
    )
    diff_dens_list = np.array(diff_dens_list)
    diff_dens_list2 = np.copy(diff_dens_list)
    #print(diff_dens_list.shape, dip_mat.shape)
    #print(
    dip_mat2 = np.copy(dip_mat)
    tilde_mom = diff_dens_list[np.newaxis, :, :, :] * dip_mat[:,
                                                              np.newaxis, :, :]
    tilde_mom = tilde_mom.sum(axis=(2, 3))
    print()
    print("#######################################################")
    print(
        "moment matrix of response densities obtained by the electron density difference of  single point calculation with and without an external potential "
    )
    print(tilde_mom * 10000)
    print("#######################################################")
    tilde_mom = diff_dens_list2[
        np.newaxis, :, :, :] * dip_mat2[:, np.newaxis, :, :]
    tilde_mom = tilde_mom.sum(axis=(2, 3))
    tilde_mom = (tilde_mom + tilde_mom.T) / 2
    chol_left = np.linalg.cholesky(tilde_mom * -1)
    #chol_right = chol_left
    chol_right = chol_left.T
    inv_chol_right = np.linalg.inv(chol_right)
    print("#######################################################")
    print("MES moment  matrix from choleky decompostion:")
    print("#######################################################")
    print(chol_right * 10000)
    #print()
    #print(inv_chol_right)
    mom_states = np.zeros(diff_dens_list2.shape)
    #for k in range(3)
    for i in range(3):
        for j in range(3):
            #mom_states[i] = inv_chol_right[i,j] * diff_dens_list[j]
            mom_states[i] += inv_chol_right[j, i] * diff_dens_list2[j]
    print("MES moment matrix obtaned by integrating over MES")
    #mom_mat = mom_states[np.newaxis,:,:,:] * dip_mat2[:, np.newaxis ,:,:]
    mom_mat = dip_mat2[np.newaxis, :, :, :] * mom_states[:, np.newaxis, :, :]
    mom_mat = mom_mat.sum(axis=(2, 3))
    print(mom_mat * 10000)

    #tilde_mom = diff_dens_list2[np.newaxis, :,:,:] * dip_mat2[:, np.newaxis ,:,:]
    #tilde_mom = tilde_mom.sum(axis = (2,3))
    #print("HIST")
    #print(tilde_mom*10000)
    print()
    print("saving density matrix of moment expanded states ... ")
    for j in range(mom_states.shape[0]):
        fn_mes = "mes_" + str(j + 1)
        np.savetxt(fn_mes, mom_states[j])
        print(fn_mes)