Beispiel #1
0
def density(mol, outfile, dm, nx=80, ny=80, nz=80):
    coord = mol.atom_coords()
    box = numpy.max(coord, axis=0) - numpy.min(coord, axis=0) + 4
    boxorig = numpy.min(coord, axis=0) - 2
    xs = numpy.arange(nx) * (box[0] / nx)
    ys = numpy.arange(ny) * (box[1] / ny)
    zs = numpy.arange(nz) * (box[2] / nz)
    coords = lib.cartesian_prod([xs, ys, zs])
    coords = numpy.asarray(coords, order="C") - (-boxorig)

    nao = mol.nao_nr()
    ngrids = nx * ny * nz
    blksize = min(200, ngrids)
    rho = numpy.empty(ngrids)
    for ip0, ip1 in gen_grid.prange(0, ngrids, blksize):
        ao = numint.eval_ao(mol, coords[ip0:ip1])
        rho[ip0:ip1] = numint.eval_rho(mol, ao, dm)
    rho = rho.reshape(nx, ny, nz)

    with open(outfile, "w") as f:
        f.write("Density in real space\n")
        f.write("Comment line\n")
        f.write("%5d" % mol.natm)
        f.write(" %14.8f %14.8f %14.8f\n" % tuple(boxorig.tolist()))
        f.write("%5d %14.8f %14.8f %14.8f\n" % (nx, xs[1], 0, 0))
        f.write("%5d %14.8f %14.8f %14.8f\n" % (ny, 0, ys[1], 0))
        f.write("%5d %14.8f %14.8f %14.8f\n" % (nz, 0, 0, zs[1]))
        for ia in range(mol.natm):
            chg = mol.atom_charge(ia)
            f.write("%5d %f" % (chg, chg))
            f.write(" %14.8f %14.8f %14.8f\n" % tuple(coord[ia]))
        fmt = " %14.8e" * nz + "\n"
        for ix in range(nx):
            for iy in range(ny):
                f.write(fmt % tuple(rho[ix, iy].tolist()))
Beispiel #2
0
def density_cut(mol, dm, nx=80, ny=80, z_value=0):
    from scipy.constants import physical_constants
    from pyscf import lib
    from pyscf.dft import gen_grid, numint

    nz = 1

    coord = mol.atom_coords()
    box = np.max(coord,axis=0) - np.min(coord,axis=0) + 6
    boxorig = np.min(coord,axis=0) - 3
    xs = np.arange(nx) * (box[0]/nx)
    ys = np.arange(ny) * (box[1]/ny)
    zs = np.array([z_value]) 
    #zs = np.arange(nz) * (box[2]/nz)
    coords = lib.cartesian_prod([xs,ys,zs])
    coords = np.asarray(coords, order='C') - (-boxorig)

    ngrids = nx * ny * nz
    blksize = min(8000, ngrids)
    rho = np.empty(ngrids)
    ao = None
    for ip0, ip1 in gen_grid.prange(0, ngrids, blksize):
        ao = numint.eval_ao(mol, coords[ip0:ip1], out=ao)
        rho[ip0:ip1] = numint.eval_rho(mol, ao, dm)
    rho = rho.reshape(nx,ny)

    # needed for conversion as x,y are in bohr for some reason
    a0 = physical_constants["Bohr radius"][0]

    return rho.T, (xs + boxorig[0]) * a0, (ys + boxorig[1]) * a0
Beispiel #3
0
def density(mol, outfile, dm, nx=80, ny=80, nz=80):
    coord = [mol.atom_coord(ia) for ia in range(mol.natm)]
    box = numpy.max(coord,axis=0) - numpy.min(coord,axis=0) + 4
    boxorig = numpy.min(coord,axis=0) - 2
    xs = numpy.arange(nx) * (box[0]/nx)
    ys = numpy.arange(ny) * (box[1]/ny)
    zs = numpy.arange(nz) * (box[2]/nz)
    coords = numpy.vstack(numpy.meshgrid(xs,ys,zs)).reshape(3,-1).T
    coords = numpy.asarray(coords, order='C') - (-boxorig)

    nao = mol.nao_nr()
    ngrids = nx * ny * nz
    blksize = min(200, ngrids)
    rho = numpy.empty(ngrids)
    for ip0, ip1 in gen_grid.prange(0, ngrids, blksize):
        ao = numint.eval_ao(mol, coords[ip0:ip1])
        rho[ip0:ip1] = numint.eval_rho(mol, ao, dm)
    rho = rho.reshape(nx,ny,nz)

    with open(outfile, 'w') as f:
        f.write('Density in real space\n')
        f.write('Comment line\n')
        f.write('%5d' % mol.natm)
        f.write(' %14.8f %14.8f %14.8f\n' % tuple(boxorig.tolist()))
        f.write('%5d %14.8f %14.8f %14.8f\n' % (nx, xs[1], 0, 0))
        f.write('%5d %14.8f %14.8f %14.8f\n' % (ny, 0, ys[1], 0))
        f.write('%5d %14.8f %14.8f %14.8f\n' % (nz, 0, 0, zs[1]))
        for ia in range(mol.natm):
            chg = mol.atom_charge(ia)
            f.write('%5d %f' % (chg, chg))
            f.write(' %14.8f %14.8f %14.8f\n' % tuple(mol.atom_coord(ia).tolist()))
        fmt = ' %14.8e' * nz + '\n'
        for ix in range(nx):
            for iy in range(ny):
                f.write(fmt % tuple(rho[ix,iy].tolist()))
Beispiel #4
0
  def test_cube_c(self):
    """ Compute the density and store into a cube file  """

    # Initialize the class cube_c
    cc = Cube(mol, nx=20, ny=20, nz=20)
    
    # Compute density on the .cube grid
    coords = cc.get_coords()
    ngrids = cc.get_ngrids()
    blksize = min(8000, ngrids)
    rho = np.empty(ngrids)
    ao = None
    dm = mf.make_rdm1()
    for ip0, ip1 in gen_grid.prange(0, ngrids, blksize):
        ao = numint.eval_ao(mol, coords[ip0:ip1], out=ao)
        rho[ip0:ip1] = numint.eval_rho(mol, ao, dm)
    rho = rho.reshape(cc.nx,cc.ny,cc.nz)
    
    # Write out density to the .cube file
    cc.write(rho, "h2o_den_cube_c.cube", comment='Electron density in real space (e/Bohr^3)')
Beispiel #5
0
def density(mol, outfile, dm, nx=80, ny=80, nz=80, pad=4.0, gridspacing=None):
    """Calculates electron density.

    Args:
        mol (Mole): Molecule to calculate the electron density for.
        outfile (str): Name of Cube file to be written.
        dm (str): Density matrix of molecule.
        nx (int): Number of grid point divisions in x direction.
           Note this is function of the molecule's size; a larger molecule
           will have a coarser representation than a smaller one for the
           same value.
        ny (int): Number of grid point divisions in y direction.
        nz (int): Number of grid point divisions in z direction.
        pad (float): Amount of padding (in Angstrom) in all dimensions that will
                     be applied in the automatic construction of the rectangular
                     grid volume based on the geometry of the system.
        gridspacing (float):  Distance, in Angstroms, between points in the grid,
                              in all dimensions. This will override the nx,ny,nz
                              the parameters. Note the following values:
               value/Angstroms  points/Bohr     Gaussian grid term
               0.1763           3                       Coarse
               0.0882           6                       Medium
               0.0441           12                      Fine
    """
    grid = grid_utils.Grid(mol, nx, ny, nz, pad, gridspacing)

    ngrids = grid.coords.shape[0]
    blksize = min(8000, ngrids)
    rho = numpy.empty(ngrids)
    ao = None
    for ip0, ip1 in gen_grid.prange(0, ngrids, blksize):
        ao = numint.eval_ao(mol, grid.coords[ip0:ip1], out=ao)
        rho[ip0:ip1] = numint.eval_rho(mol, ao, dm)
    rho = rho.reshape(grid.nx, grid.ny, grid.nz)

    grid_utils.write_formatted_cube_file(
        outfile, 'Electron density in real space (e/Bohr^3)', grid, rho)
Beispiel #6
0
def density(mol, outfile, dm, nx=80, ny=80, nz=80):
    coord = mol.atom_coords()
    box = numpy.max(coord, axis=0) - numpy.min(coord, axis=0) + 6
    boxorig = numpy.min(coord, axis=0) - 3
    xs = numpy.arange(nx) * (box[0] / nx)
    ys = numpy.arange(ny) * (box[1] / ny)
    zs = numpy.arange(nz) * (box[2] / nz)
    coords = lib.cartesian_prod([xs, ys, zs])
    coords = numpy.asarray(coords, order='C') - (-boxorig)

    nao = mol.nao_nr()
    ngrids = nx * ny * nz
    blksize = min(200, ngrids)
    rho = numpy.empty(ngrids)
    for ip0, ip1 in gen_grid.prange(0, ngrids, blksize):
        ao = numint.eval_ao(mol, coords[ip0:ip1])
        rho[ip0:ip1] = numint.eval_rho(mol, ao, dm)
    rho = rho.reshape(nx, ny, nz)

    with open(outfile, 'w') as f:
        f.write('Electron density in real space (e/Bohr^3)\n')
        f.write('PySCF Version: %s  Date: %s\n' %
                (pyscf.__version__, time.ctime()))
        f.write('%5d' % mol.natm)
        f.write(' %14.8f %14.8f %14.8f\n' % tuple(boxorig.tolist()))
        f.write('%5d %14.8f %14.8f %14.8f\n' % (nx, xs[1], 0, 0))
        f.write('%5d %14.8f %14.8f %14.8f\n' % (ny, 0, ys[1], 0))
        f.write('%5d %14.8f %14.8f %14.8f\n' % (nz, 0, 0, zs[1]))
        for ia in range(mol.natm):
            chg = mol.atom_charge(ia)
            f.write('%5d %f' % (chg, chg))
            f.write(' %14.8f %14.8f %14.8f\n' % tuple(coord[ia]))
        fmt = ' %14.8e' * nz + '\n'
        for ix in range(nx):
            for iy in range(ny):
                f.write(fmt % tuple(rho[ix, iy].tolist()))
Beispiel #7
0
def isomep(mol,
           outfile,
           dm,
           electronic_iso=0.002,
           iso_tol=0.00003,
           nx=80,
           ny=80,
           nz=80,
           pad=4.0,
           gridspacing=None):
    """Calculates MEP on a specific electron density surface.

    Args:
        mol (Mole): Molecule to calculate the electron density for.
        outfile (str): Name of Cube file to be written.
        dm (str): Density matrix of molecule.
        nx (int): Number of grid point divisions in x direction.
           Note this is function of the molecule's size; a larger molecule
           will have a coarser representation than a smaller one for the
           same value.
        ny (int): Number of grid point divisions in y direction.
        nz (int): Number of grid point divisions in z direction.
        pad (float): Amount of padding (in Angstrom) in all dimensions that will
                     be applied in the automatic construction of the rectangular
                     grid volume based on the geometry of the system.
        gridspacing (float):  Distance, in Angstroms, between points in the grid,
                              in all dimensions. This will override the nx,ny,nz
                              the parameters. Note the following values:
               value/Angstroms  points/Bohr     Gaussian grid term
               0.1763           3                       Coarse
               0.0882           6                       Medium
               0.0441           12                      Fine

    """
    grid = grid_utils.Grid(mol, nx, ny, nz, pad, gridspacing)
    LOGGER.debug("grid coords shape: %s", grid.coords.shape)
    LOGGER.debug("grid coords first element shape: %s", grid.coords[0].shape)
    ngrids = grid.coords.shape[0]
    blksize = min(8000, ngrids)
    rho = numpy.empty(ngrids)
    ao = None
    for ip0, ip1 in gen_grid.prange(0, ngrids, blksize):
        ao = numint.eval_ao(mol, grid.coords[ip0:ip1], out=ao)
        rho[ip0:ip1] = numint.eval_rho(mol, ao, dm)

    LOGGER.note("Total number of density voxels: %d", len(rho))

    is_surface_voxel = numpy.logical_and(rho > (electronic_iso - iso_tol),
                                         rho < (electronic_iso + iso_tol))
    LOGGER.debug("Surface voxel count from logical_and: %d",
                 numpy.count_nonzero(is_surface_voxel))

    surface_voxel_grid_indices = numpy.nonzero(is_surface_voxel)[0]
    LOGGER.debug2("Surface voxel indices: %s", surface_voxel_grid_indices)
    LOGGER.debug2("Surface voxel indices shape: %s",
                  surface_voxel_grid_indices.shape)

    # Number of voxels at the defined electronic_iso surface
    # Used for area
    # Voxels at surface
    #(num > (ISO - TOL) ) && (num < (ISO + TOL) )
    surface_voxel_count = surface_voxel_grid_indices.shape[0]
    surface_voxel_coords = grid.coords[surface_voxel_grid_indices[0]]
    for i in range(1, surface_voxel_grid_indices.shape[0]):
        surface_voxel_coord = grid.coords[surface_voxel_grid_indices[i]]
        LOGGER.debug4("surface voxel coord shape: %s",
                      surface_voxel_coord.shape)
        surface_voxel_coords = numpy.append(surface_voxel_coords,
                                            surface_voxel_coord,
                                            axis=0)
    LOGGER.debug("surface_voxel_coords shape: %s", surface_voxel_coords.shape)
    surface_voxel_coords = surface_voxel_coords.reshape(
        (surface_voxel_grid_indices.shape[0], 3))
    LOGGER.debug("surface_voxel_coords shape: %s", surface_voxel_coords.shape)
    LOGGER.debug3("First coord from grid: %s",
                  grid.coords[surface_voxel_grid_indices[0]])
    LOGGER.debug3("First coord from surf voxel array: %s",
                  surface_voxel_coords[0])

    is_in_surface = numpy.greater(rho, electronic_iso)
    LOGGER.debug("Voxel count from numpy.greater: %d",
                 numpy.count_nonzero(is_in_surface))
    # Number of voxels *within* the defined electronic_iso surface
    # Used for volume
    inner_voxel_count = numpy.count_nonzero(is_in_surface)
    # This time, actually change
    LOGGER.debug("rho shape: %s", rho.shape)
    rho = rho.reshape(grid.nx, grid.ny, grid.nz)
    LOGGER.debug("rho shape: %s", rho.shape)

    LOGGER.note("surface voxels_found: %d", surface_voxel_count)
    voxel_area = gridspacing * gridspacing
    LOGGER.info("Each voxel area /  A^2: %f", voxel_area)
    LOGGER.info("inner surface area / A^2: %f",
                surface_voxel_count * voxel_area)

    LOGGER.info("inner voxel count: %d", inner_voxel_count)
    voxel_volume = gridspacing * gridspacing * gridspacing
    LOGGER.info("Each voxel volume /  A^3: %f", voxel_volume)
    inner_volume = inner_voxel_count * voxel_volume
    LOGGER.info("Total inner volume / A^3: %f", inner_volume)

    mep_values = mep_for_coords(mol, dm, surface_voxel_coords)
    LOGGER.debug("MEP values shape: %s", mep_values.shape)
    mep_values = mep_values.reshape((mep_values.shape[0], 1))
    LOGGER.debug("MEP values shape: %s", mep_values.shape)
    #Add the potentials to the coordinates: each row describes one point.
    coords_with_mep_values = numpy.append(surface_voxel_coords,
                                          mep_values,
                                          axis=1)

    cube_information = "Molecular electrostatic potential in real space on {:.5f} isodensity surface. Volume: {:.6f}".format(
        electronic_iso, inner_volume)

    grid_utils.write_unformatted_cube_file(outfile, cube_information, grid,
                                           coords_with_mep_values)
Beispiel #8
0
def density(mol, outfile, dm, nx=80, ny=80, nz=80):
    """Calculates electron density.

    Args:
        mol (Mole): Molecule to calculate the electron density for.
        outfile (str): Name of Cube file to be written.
        dm (str): Density matrix of molecule.
        nx (int): Number of grid point divisions in x direction.
           Note this is function of the molecule's size; a larger molecule
           will have a coarser representation than a smaller one for the
           same value.
        ny (int): Number of grid point divisions in y direction.
        nz (int): Number of grid point divisions in z direction.


    """

    coord = mol.atom_coords()
    box = numpy.max(coord, axis=0) - numpy.min(coord, axis=0) + 6
    boxorig = numpy.min(coord, axis=0) - 3
    xs = numpy.arange(nx) * (box[0] / nx)
    ys = numpy.arange(ny) * (box[1] / ny)
    zs = numpy.arange(nz) * (box[2] / nz)
    coords = lib.cartesian_prod([xs, ys, zs])
    coords = numpy.asarray(coords, order='C') - (-boxorig)

    ngrids = nx * ny * nz
    blksize = min(8000, ngrids)
    rho = numpy.empty(ngrids)
    ao = None
    for ip0, ip1 in gen_grid.prange(0, ngrids, blksize):
        ao = numint.eval_ao(mol, coords[ip0:ip1], out=ao)
        rho[ip0:ip1] = numint.eval_rho(mol, ao, dm)
    rho = rho.reshape(nx, ny, nz)

    with open(outfile, 'w') as f:
        f.write('Electron density in real space (e/Bohr^3)\n')
        f.write('PySCF Version: %s  Date: %s\n' %
                (pyscf.__version__, time.ctime()))
        f.write('%5d' % mol.natm)
        f.write('%12.6f%12.6f%12.6f\n' % tuple(boxorig.tolist()))
        f.write('%5d%12.6f%12.6f%12.6f\n' % (nx, xs[1], 0, 0))
        f.write('%5d%12.6f%12.6f%12.6f\n' % (ny, 0, ys[1], 0))
        f.write('%5d%12.6f%12.6f%12.6f\n' % (nz, 0, 0, zs[1]))
        for ia in range(mol.natm):
            chg = mol.atom_charge(ia)
            f.write('%5d%12.6f' % (chg, chg))
            f.write('%12.6f%12.6f%12.6f\n' % tuple(coord[ia]))

        for ix in range(nx):
            for iy in range(ny):
                for iz in range(0, nz, 6):
                    remainder = (nz - iz)
                    if (remainder > 6):
                        fmt = '%13.5E' * 6 + '\n'
                        f.write(fmt % tuple(rho[ix, iy, iz:iz + 6].tolist()))
                    else:
                        fmt = '%13.5E' * remainder + '\n'
                        f.write(fmt %
                                tuple(rho[ix, iy, iz:iz + remainder].tolist()))
                        break