def test_cell_distances1_exclude_a(): cell = Cell(np.zeros((0, 3), float)) pos0 = np.array([ [1.0, 0.0, 0.0], [0.0, 0.0, 0.0], ]) # First output = np.zeros(1, float) exclude = np.zeros((0, 2), int) cell.compute_distances(output, pos0, pairs=exclude) assert output[0] == 1 # Second output = np.zeros(0, float) exclude = np.array([[1, 0]]) cell.compute_distances(output, pos0, pairs=exclude) # Third output = np.zeros(0, float) exclude = np.array([[0, 1]]) try: cell.compute_distances(output, pos0, pairs=exclude) assert False except ValueError: pass
def test_cell_distances2_exclude_a(): cell = Cell(np.zeros((0, 3), float)) pos0 = np.array([ [0.0, 0.0, 0.0], ]) pos1 = np.array([ [0.0, 0.0, 1.0], ]) # Zeroth output = np.zeros(1, float) cell.compute_distances(output, pos0, pos1) assert output[0] == 1 # First output = np.zeros(0, float) exclude = np.array([[0, 0]]) cell.compute_distances(output, pos0, pos1, pairs=exclude) # Second output = np.zeros(0, float) for exclude in np.array([[-1, 0]]), np.array([[0, -1]]), np.array( [[0, 5]]), np.array([[1, 0]]): print exclude try: cell.compute_distances(output, pos0, pos1, pairs=exclude) assert False except ValueError: pass
def test_cell_distances1_exclude_b(): cell = Cell(np.zeros((0,3),float)) pos0 = np.array([ [1.0, 0.0, 0.0], [0.0, 0.0, 0.0], [2.0, 0.0, 0.0], ]) # Zeroth output = np.zeros(3, float) cell.compute_distances(output, pos0) assert output[0] == 1 assert output[1] == 1 assert output[2] == 2 # First output = np.zeros(2, float) exclude = np.array([[1,0]]) cell.compute_distances(output, pos0, pairs=exclude) assert output[0] == 1 assert output[1] == 2 # Second output = np.zeros(1, float) exclude = np.array([[1,0],[2,1]]) cell.compute_distances(output, pos0, pairs=exclude) assert output[0] == 1 # Third output = np.zeros(1, float) exclude = np.array([[2,1],[1,0]]) with assert_raises(ValueError): cell.compute_distances(output, pos0, pairs=exclude)
def test_cell_water32(): cell = get_system_water32().cell assert (cell.rspacings == 9.865*angstrom).all() assert (cell.gspacings == 1/(9.865*angstrom)).all() assert abs(cell.volume - abs(np.linalg.det(cell.rvecs))) < 1e-10 assert abs(np.dot(cell.gvecs, cell.rvecs.transpose()) - np.identity(3)).max() < 1e-5 assert abs(np.dot(cell.gvecs.transpose(), cell.rvecs) - np.identity(3)).max() < 1e-5 vec1 = np.array([10.0, 0.0, 5.0])*angstrom cell.mic(vec1) assert abs(vec1 - np.array([0.135, 0.0, -4.865])*angstrom).max() < 1e-10 vec2 = np.array([10.0, 0.0, 5.0])*angstrom cell.add_vec(vec2, cell.to_center(vec2)) assert abs(vec1 - vec2).max() < 1e-10 cell.add_vec(vec1, np.array([1,2,3])) assert abs(vec1 - np.array([10.0, 19.73, 24.73])*angstrom).max() < 1e-10 cell2 = Cell(-cell.rvecs) assert abs(cell2.volume - abs(np.linalg.det(cell.rvecs))) < 1e-10
def test_cell_distances2_exclude_b(): cell = Cell(np.zeros((0, 3), float)) pos0 = np.array([ [0.0, 0.0, 0.0], [0.0, 0.0, 1.0], ]) pos1 = np.array([ [0.0, 0.0, 1.0], [0.0, 0.0, 2.0], ]) # Zeroth output = np.zeros(4, float) cell.compute_distances(output, pos0, pos1) assert output[0] == 1 assert output[1] == 2 assert output[2] == 0 assert output[3] == 1 # First output = np.zeros(3, float) exclude = np.array([[0, 0]]) cell.compute_distances(output, pos0, pos1, pairs=exclude) assert output[0] == 2 assert output[1] == 0 assert output[2] == 1 # Second output = np.zeros(2, float) exclude = np.array([[0, 0], [1, 1]]) cell.compute_distances(output, pos0, pos1, pairs=exclude) assert output[0] == 2 assert output[1] == 0 # Third output = np.zeros(2, float) for exclude in np.array([[1, 0], [0, 1]]), np.array([[1, 0], [0, 0]]): try: cell.compute_distances(output, pos0, pos1, pairs=exclude) assert False except ValueError: pass
def test_align_cell_quartz(): system = get_system_quartz() system.cell = Cell(system.cell.rvecs[::-1].copy()) lcs = np.array([ [1, 1, 0], [0, 0, 1], ]) system.align_cell(lcs) # c should be aligned with z axis rvecs = system.cell.rvecs assert abs(rvecs[2][0]) < 1e-10 assert abs(rvecs[2][1]) < 1e-10 # sum of a and b should be aligned with x axis assert abs(rvecs[0][1] + rvecs[1][1]) < 1e-10 assert abs(rvecs[0][2] + rvecs[1][2]) < 1e-10 # difference of a and b should be aligned with y axis assert abs(rvecs[0][0] - rvecs[1][0]) < 1e-4 assert abs(rvecs[0][2] - rvecs[1][2]) < 1e-10 # check if the bonds are the same in the rotated structure check_detect_bonds(system)
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()
def qff(args=None): if args is None: args = qff_parse_args() else: args = qff_parse_args(args) #define logger verbosity = None if args.silent: verbosity = 'silent' else: if args.very_verbose: verbosity = 'highest' elif args.verbose: verbosity = 'high' #get settings kwargs = { 'fn_traj': args.fn_traj, 'only_traj': args.only_traj, 'program_mode': args.program_mode, 'plot_traj': args.plot_traj, 'xyz_traj': args.xyz_traj, 'suffix': args.suffix, 'log_level': verbosity, 'log_file': args.logfile, 'ffatypes': args.ffatypes, 'ei': args.ei, 'ei_rcut': args.ei_rcut, 'vdw': args.vdw, 'vdw_rcut': args.vdw_rcut, 'covres': args.covres, } settings = Settings(fn=args.config_file, **kwargs) with log.section('INIT', 1, timer='Initializing'): log.dump('Initializing system') #read system and ab initio reference system = None energy = 0.0 grad = None hess = None pbc = None rvecs = None for fn in args.fn: 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 list(sample.keys()): energy = sample['energy'] if 'grad' in list(sample.keys()): grad = sample['grad'] elif 'gradient' in list(sample.keys()): grad = sample['gradient'] if 'hess' in list(sample.keys()): hess = sample['hess'] elif 'hessian' in list(sample.keys()): hess = sample['hessian'] if 'rvecs' in list(sample.keys()): pbc = [1, 1, 1] else: pbc = [0, 0, 0] if system is None: system = System.from_file(fn) else: if 'pos' in list(sample.keys()): system.pos = sample['pos'] elif 'coords' in list(sample.keys()): system.pos = sample['coords'] if 'rvecs' in list(sample.keys()): system.cell = Cell(sample['rvecs']) elif 'cell' in list(sample.keys()): system.cell = Cell(sample['cell']) if 'bonds' in list(sample.keys()): system.bonds = sample['bonds'] if 'ffatypes' in list(sample.keys()): system.ffatypes = sample['ffatypes'] if 'ffatype_ids' in list(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 settings.ffatypes is not None: set_ffatypes(system, settings.ffatypes) else: raise AssertionError('No atom types defined') if settings.do_hess_negfreq_proj: log.dump( 'Projecting negative frequencies out of the mass-weighted hessian.' ) with log.section('SYS', 3, 'Initializing'): hess = project_negative_freqs(hess, system.masses) #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 settings.ei is not None: if rvecs is None: if settings.ei_rcut is None: rcut = 50 * angstrom else: rcut = settings.ei_rcut ff = ForceField.generate(system, settings.ei, rcut=rcut) else: if settings.ei_rcut is None: rcut = 20 * angstrom else: rcut = settings.ei_rcut ff = ForceField.generate(system, settings.ei, rcut=rcut, alpha_scale=3.2, gcut_scale=1.5, smooth_ei=True) refs.append(YaffForceField('EI', ff)) if settings.vdw is not None: ff = ForceField.generate(system, settings.vdw, rcut=settings.vdw_rcut) refs.append(YaffForceField('vdW', ff)) if settings.covres is not None: ff = ForceField.generate(system, settings.covres) refs.append(YaffForceField('Cov res', ff)) #define quickff program assert settings.program_mode in allowed_programs, \ 'Given program mode %s not allowed. Choose one of %s' %( settings.program_mode, ', '.join([prog for prog in allowed_programs if not prog=='BaseProgram']) ) mode = program_modes[settings.program_mode] program = mode(system, ai, settings, ffrefs=refs) #run program program.run() return program