Example #1
0
import shutil
import tempfile

import h5py as h5

from quickff.io import read_abinitio

from quickff.context import context
from quickff.io import read_abinitio
from quickff.reference import SecondOrderTaylor
from quickff.valence import ValenceFF

from yaff import System

from quickff.log import log
log.set_level('silent')

__all__ = ['log', 'read_system', 'tmpdir']

def read_system(name):
    # Load system data.
    fn = context.get_fn(os.path.join('systems', name))
    numbers, coords, energy, grad, hess, masses, rvecs, pbc = read_abinitio(fn)
    # Try to load charges.
    charges = None
    fns_wpart = glob(os.path.join(os.path.dirname(fn), 'gaussian_mbis.h5'))
    if len(fns_wpart) > 0:
        with h5.File(fns_wpart[0], 'r') as f:
            charges = f['charges'][:]
    # Create system object.
    system = System(numbers, coords, charges=charges)
Example #2
0
import shutil
import tempfile

import h5py as h5

from quickff.io import read_abinitio

from quickff.context import context
from quickff.io import read_abinitio
from quickff.reference import SecondOrderTaylor
from quickff.valence import ValenceFF

from yaff import System

from quickff.log import log
log.set_level('silent')

__all__ = ['log', 'read_system', 'tmpdir']

def read_system(name):
    # Load system data.
    fn = context.get_fn(os.path.join('systems', name))
    numbers, coords, energy, grad, hess, masses, rvecs, pbc = read_abinitio(fn)
    # Try to load charges.
    charges = None
    fns_wpart = glob(os.path.join(os.path.dirname(fn), 'gaussian_mbis.h5'))
    if len(fns_wpart) > 0:
        with h5.File(fns_wpart[0], 'r') as f:
            charges = f['charges'][:]
    # Create system object.
    system = System(numbers, coords, charges=charges)
Example #3
0
def main():
    options, fns = parse()
    #define logger
    if options.silent:
        log.set_level('silent')
    else:
        if options.very_verbose:
            log.set_level('highest')
        elif options.verbose:
            log.set_level('high')
        if options.logfile is not None and isinstance(options.logfile, str):
            log.write_to_file(options.logfile)
    with log.section('QFF', 1, timer='Initializing'):
        log.dump('Initializing system')
        #read system and ab initio reference
        system = None
        energy = 0.0
        grad = None
        hess = None
        rvecs = None
        for fn in fns:
            if fn.endswith('.fchk') or fn.endswith('.xml'):
                numbers, coords, energy, grad, hess, masses, rvecs, pbc = read_abinitio(
                    fn)
                if system is None:
                    system = System(numbers,
                                    coords,
                                    rvecs=rvecs,
                                    charges=None,
                                    radii=None,
                                    masses=masses)
                else:
                    system.pos = coords.copy()
                    system.cell = Cell(rvecs)
                    system.numbers = numbers.copy()
                    if masses is not None: system.masses = masses.copy()
                    system._init_derived()
            elif fn.endswith('.chk'):
                sample = load_chk(fn)
                if 'energy' in sample.keys(): energy = sample['energy']
                if 'grad' in sample.keys(): grad = sample['grad']
                elif 'gradient' in sample.keys(): grad = sample['gradient']
                if 'hess' in sample.keys(): hess = sample['hess']
                elif 'hessian' in sample.keys(): hess = sample['hessian']
                if system is None:
                    system = System.from_file(fn)
                else:
                    if 'pos' in sample.keys(): system.pos = sample['pos']
                    elif 'coords' in sample.keys():
                        system.pos = sample['coords']
                    if 'rvecs' in sample.keys():
                        system.cell = Cell(sample['rvecs'])
                    elif 'cell' in sample.keys():
                        system.cell = Cell(sample['cell'])
                    if 'bonds' in sample.keys(): system.bonds = sample['bonds']
                    if 'ffatypes' in sample.keys():
                        system.ffatypes = sample['ffatypes']
                    if 'ffatype_ids' in sample.keys():
                        system.ffatype_ids = sample['ffatype_ids']
                    system._init_derived()
            else:
                raise NotImplementedError('File format for %s not supported' %
                                          fn)
        assert system is not None, 'No system could be defined from input'
        assert grad is not None, 'No ab initio gradient found in input'
        assert hess is not None, 'No ab initio hessian found in input'
        #complete the system information
        if system.bonds is None: system.detect_bonds()
        if system.masses is None: system.set_standard_masses()
        if system.ffatypes is None:
            if options.ffatypes in ['low', 'medium', 'high', 'highest']:
                guess_ffatypes(system, options.ffatypes)
            elif options.ffatypes is not None:
                raise NotImplementedError(
                    'Guessing atom types from %s not implemented' %
                    options.ffatypes)
            else:
                raise AssertionError('No atom types defined')
        #construct ab initio reference
        ai = SecondOrderTaylor('ai',
                               coords=system.pos.copy(),
                               energy=energy,
                               grad=grad,
                               hess=hess,
                               pbc=pbc)
        #detect a priori defined contributions to the force field
        refs = []
        if options.ei is not None:
            if rvecs is None:
                ff = ForceField.generate(system,
                                         options.ei,
                                         rcut=50 * angstrom)
            else:
                ff = ForceField.generate(system,
                                         options.ei,
                                         rcut=20 * angstrom,
                                         alpha_scale=3.2,
                                         gcut_scale=1.5,
                                         smooth_ei=True)
            refs.append(YaffForceField('EI', ff))
        if options.vdw is not None:
            ff = ForceField.generate(system, options.vdw, rcut=20 * angstrom)
            refs.append(YaffForceField('vdW', ff))
        if options.covres is not None:
            ff = ForceField.generate(system, options.covres)
            refs.append(YaffForceField('Cov res', ff))
    #define quickff program
    assert options.program_mode in allowed_programs, \
        'Given program mode %s not allowed. Choose one of %s' %(
            options.program_mode,
            ', '.join([prog for prog in allowed_programs if not prog=='BaseProgram'])
        )
    mode = program_modes[options.program_mode]
    only_traj = 'PT_ALL'
    if options.only_traj is not None: only_traj = options.only_traj.split(',')
    program = mode(system,
                   ai,
                   ffrefs=refs,
                   fn_traj=options.fn_traj,
                   only_traj=only_traj,
                   plot_traj=options.ener_traj,
                   xyz_traj=options.xyz_traj,
                   suffix=options.suffix)
    #run program
    program.run()
Example #4
0
 def _set_log(self):
     log.set_level(self.log_level)
     f = self.log_file
     if f is not None and (isinstance(f, str) or isinstance(f, IOBase)):
         log.write_to_file(f)
Example #5
0
 def _set_log(self):
     log.set_level(self.log_level)
     f = self.log_file
     if f is not None and (isinstance(f, str) or isinstance(f, IOBase)):
         log.write_to_file(f)