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

    # Load system file
    if args.fn_sys.endswith('.fchk'):
        numbers, coords, energy, grad, hess, masses, rvecs, pbc = read_abinitio(
            args.fn_sys, do_hess=False)
        system = System(numbers,
                        coords,
                        rvecs=None,
                        charges=None,
                        radii=None,
                        masses=masses)
        system.detect_bonds()
    else:
        system = System.from_file(args.fn_sys)

    # Guess atom types if needed
    if args.ffatypes is not None:
        guess_ffatypes(system, args.ffatypes)
    ffatypes = [system.ffatypes[i] for i in system.ffatype_ids]

    # Load atomic charges
    fn_charges, _, path = args.charges.partition(':')
    if fn_charges.endswith('.h5'):
        with h5.File(fn_charges, 'r') as f:
            if not path in f:
                raise IOError(
                    'Given HDF5 file %s does not contain a dataset %s' %
                    (fn_charges, path))
            charges = f[path][:]
            radii = None
            if args.gaussian:
                path_radii = os.path.join(os.path.dirname(path), 'radii')
                if 'radii' in f[path]:
                    radii = average(f['%s/radii' % path][:],
                                    ffatypes,
                                    fmt='dict')
                else:
                    radii = average(get_ei_radii(system.numbers),
                                    ffatypes,
                                    fmt='dict')
    elif fn_charges.endswith('.chk'):
        sample = load_chk(fn_charges)
        if path in sample.keys():
            charges = sample[path]
        else:
            raise IOError(
                'Given CHK file %s does not contain a dataset with label %s' %
                (fn_charges, path))
        radii = None
        if args.gaussian:
            if 'radii' in sample.keys():
                radii = average(sample['radii'], ffatypes, fmt='dict')
    else:
        raise IOError(
            'Invalid extension, fn_charges should be a HDF5 or a CHK file.')

    # Derive charge parameters
    if args.bci:
        constraints = {}
        if args.bci_constraints is not None:
            constraints = read_bci_constraints(args.bci_constraints)
        bcis = charges_to_bcis(charges,
                               ffatypes,
                               system.bonds,
                               constraints=constraints,
                               verbose=args.verbose)
        make_yaff_ei(args.fn_out, None, bcis=bcis, radii=radii)
    else:
        charges = average(charges, ffatypes, fmt='dict', verbose=args.verbose)
        make_yaff_ei(args.fn_out, charges, radii=radii)
Exemple #2
0
def main():
    options, args = parse()
    fn_sys, fn_in, path = args
    if fn_sys.endswith('.fchk'):
        numbers, coords, energy, grad, hess, masses, rvecs, pbc = read_abinitio(
            fn_sys, do_hess=False)
        system = System(numbers,
                        coords,
                        rvecs=None,
                        charges=None,
                        radii=None,
                        masses=masses)
        system.detect_bonds()
    else:
        system = System.from_file(fn_sys)
    if options.ffatypes is not None:
        guess_ffatypes(system, options.ffatypes)
    ffatypes = [system.ffatypes[i] for i in system.ffatype_ids]
    if fn_in.endswith('.h5'):
        h5 = h5py.File(fn_in)
        if not path in h5 or 'charges' not in h5[path]:
            raise IOError(
                'Given HDF5 file %s does not contain dataset %s/charges' %
                (fn_in, path))
        charges = h5['%s/charges' % path][:]
        radii = None
        if options.gaussian:
            if 'radii' in h5[path]:
                radii = average(h5['%s/radii' % path][:], ffatypes, fmt='dict')
            else:
                radii = average(get_ei_radii(system.numbers),
                                ffatypes,
                                fmt='dict')
    elif fn_in.endswith('.chk'):
        sample = load_chk(fn_in)
        if path in sample.keys():
            charges = sample[path]
        else:
            raise IOError(
                'Given CHK file %s does not contain dataset with label %s' %
                (fn_in, path))
        radii = None
        if options.gaussian:
            if 'radii' in sample.keys():
                radii = average(sample['radii'], ffatypes, fmt='dict')
    else:
        raise IOError(
            'Invalid extension, fn_in should be a HDF5 or a CHK file.')
    if options.output is None:
        if path == '.':
            fn_out = 'pars_ei.txt'
        else:
            fn_out = 'pars_ei_%s.txt' % path.replace('/', '_')
    else:
        fn_out = options.output
    if options.bci:
        constraints = {}
        if options.bci_constraints is not None:
            constraints = read_bci_constraints(options.bci_constraints)
        bcis = charges_to_bcis(charges,
                               ffatypes,
                               system.bonds,
                               constraints=constraints,
                               verbose=options.verbose)
        make_yaff_ei(fn_out, None, bcis=bcis, radii=radii)
    else:
        charges = average(charges,
                          ffatypes,
                          fmt='dict',
                          verbose=options.verbose)
        make_yaff_ei(fn_out, charges, radii=radii)