Exemple #1
0
def main():
    args = parse_args()

    fn_h5, grp_name = parse_h5(args.output, 'output')
    # check if the group is already present (and not empty) in the output file
    if check_output(fn_h5, grp_name, args.overwrite):
        return

    # Load the potential data
    if log.do_medium:
        log('Loading potential array')
    mol_pot = IOData.from_file(args.cube)
    if not isinstance(mol_pot.grid, UniformGrid):
        raise TypeError('The specified file does not contain data on a rectangular grid.')
    mol_pot.grid.pbc[:] = parse_pbc(args.pbc) # correct pbc
    esp = mol_pot.cube_data

    # Reduce the grid if required
    if args.stride > 1:
        esp, mol_pot.grid = reduce_data(esp, mol_pot.grid, args.stride, args.chop)

    # Fix sign
    if args.sign:
        esp *= -1

    # Some screen info
    if log.do_medium:
        log('Important parameters:')
        log.hline()
        log('Number of grid points:   %12i' % np.product(mol_pot.grid.shape))
        log('Grid shape:                 [%8i, %8i, %8i]' % tuple(mol_pot.grid.shape))
        log('PBC:                        [%8i, %8i, %8i]' % tuple(mol_pot.grid.pbc))
        log.hline()

    # Construct the weights for the ESP Cost function.
    wdens = parse_wdens(args.wdens)
    if wdens is not None:
        if log.do_medium:
            log('Loading density array')
        # either the provided density or a built-in prodensity
        rho = load_rho(mol_pot.coordinates, mol_pot.numbers, wdens[0], mol_pot.grid, args.stride, args.chop)
        wdens = (rho,) + wdens[1:]
    if log.do_medium:
        log('Constructing weight function')
    weights = setup_weights(mol_pot.coordinates, mol_pot.numbers, mol_pot.grid,
        dens=wdens,
        near=parse_wnear(args.wnear),
        far=parse_wnear(args.wfar),
    )

    # write the weights to a cube file if requested
    if args.wsave is not None:
        if log.do_medium:
            log('   Saving weights array   ')
        # construct a new data dictionary that contains all info for the cube file
        mol_weights = mol_pot.copy()
        mol_weights.cube_data = weights
        mol_weights.to_file(args.wsave)

    # rescale weights such that the cost function is the mean-square-error
    if weights.max() == 0.0:
        raise ValueError('No points with a non-zero weight were found')
    wmax = weights.min()
    wmin = weights.max()
    used_volume = mol_pot.grid.integrate(weights)

    # Some screen info
    if log.do_medium:
        log('Important parameters:')
        log.hline()
        log('Used number of grid points:   %12i' % (weights>0).sum())
        log('Used volume:                      %12.5f' % used_volume)
        log('Used volume/atom:                 %12.5f' % (used_volume/mol_pot.natom))
        log('Lowest weight:                %12.5e' % wmin)
        log('Highest weight:               %12.5e' % wmax)
        log('Max weight at edge:           %12.5f' % max_at_edge(weights, mol_pot.grid.pbc))

    # Ewald parameters
    rcut, alpha, gcut = parse_ewald_args(args)

    # Some screen info
    if log.do_medium:
        log('Ewald real cutoff:       %12.5e' % rcut)
        log('Ewald alpha:             %12.5e' % alpha)
        log('Ewald reciprocal cutoff: %12.5e' % gcut)
        log.hline()

    # Construct the cost function
    if log.do_medium:
        log('Setting up cost function (may take a while)   ')
    cost = ESPCost.from_grid_data(mol_pot.coordinates, mol_pot.grid, esp, weights, rcut, alpha, gcut)

    # Store cost function info
    results = {}
    results['cost'] = cost
    results['used_volume'] = used_volume

    # Store cost function properties
    results['evals'] = np.linalg.eigvalsh(cost._A)
    abs_evals = abs(results['evals'])
    if abs_evals.min() == 0.0:
        results['cn'] = 0.0
    else:
        results['cn'] = abs_evals.max()/abs_evals.min()

    # Report some on-screen info
    if log.do_medium:
        log('Important parameters:')
        log.hline()
        log('Lowest abs eigen value:       %12.5e' % abs_evals.min())
        log('Highest abs eigen value:      %12.5e' % abs_evals.max())
        log('Condition number:             %12.5e' % results['cn'])
        log.hline()

    # Store the results in an HDF5 file
    write_script_output(fn_h5, grp_name, results, args)
Exemple #2
0
def load_cost(arg_cost):
    '''Load an ESP cost function given at the command line'''
    fn_h5_in, grp_name_in = parse_h5(arg_cost, 'cost')
    with LockedH5File(fn_h5_in, 'r') as f:
        return ESPCost.from_hdf5(f[grp_name_in]['cost'], None), f[grp_name_in]['used_volume'][()]
Exemple #3
0
def load_cost(arg_cost):
    '''Load an ESP cost function given at the command line'''
    fn_h5_in, grp_name_in = parse_h5(arg_cost, 'cost')
    with LockedH5File(fn_h5_in, 'r') as f:
        return ESPCost.from_hdf5(
            f[grp_name_in]['cost']), f[grp_name_in]['used_volume'][()]