def test_Anchoring(self): #self.assertRaises(cs.ComplexCoordSysException, cs.RotAndTrans, numpy.array([0.,0.,0.,0.,0.,0.])) # Generates a series of rotated molecular geometries and views them. a = cs.RotAndTrans(numpy.array([1., 0., 0., 1., 1., 1.])) z = cs.ZMatrix(file2str("butane1.zmt"), anchor=a) print z.get_internals() atoms1 = z.atoms.copy() alphas = numpy.arange(0., 1., 0.01) * 2 * numpy.pi vs = numpy.arange(0., 1., 0.01) geoms_list = [] for alpha, v in zip(alphas, vs): vec = common.normalise([1, v, -v]) vec = numpy.sin(alpha / 2) * vec q = numpy.hstack([vec]) v = numpy.array([1., 1., 1.]) a.set(numpy.hstack([q, v])) geoms_list.append(z.atoms.copy()) if visual: ase.view(geoms_list)
def test_var_mask_big_water_opt(self): print "Running optimisation tests with masking of variables:" print " Lots of water molecules with internal and rotational coordinates frozen." ccs, var_types = self.form_ccs_waters(16) def gen_mask(c): if c == 'i': return False elif c == 'r': return True elif c == 'p': return True else: raise False mask = map(gen_mask, var_types) ccs.set_var_mask(mask) ccs.set_calculator(test_calc()) opt = ase.LBFGS(ccs) list = [] for i in range(8): opt.run(steps=1, fmax=0.0) list.append(ccs.atoms.copy()) if visual: ase.view(list)
def test_ComplexCoordSys(self): x = cs.XYZ(file2str("H2.xyz")) a_h2o1 = cs.RotAndTrans(numpy.array([1., 0., 0., 3., 1., 1.]), parent=x) a_h2o2 = cs.RotAndTrans(numpy.array([1., 0., 0., 1., 1., 1.]), parent=x) a_ch4 = cs.RotAndTrans(numpy.array([1., 0., 0., 1., -1., 1.]), parent=x) h2o1 = cs.ZMatrix(file2str("H2O.zmt"), anchor=a_h2o1) h2o2 = cs.ZMatrix(file2str("H2O.zmt"), anchor=a_h2o2) ch4 = cs.ZMatrix(file2str("CH4.zmt"), anchor=a_ch4) parts = [x, h2o1, h2o2, ch4] ccs = cs.ComplexCoordSys(parts) ccs.set_calculator(test_calc()) dyn = ase.LBFGS(ccs) list = [] for i in range(8): list.append(ccs.atoms.copy()) dyn.run(steps=1, fmax=0.01) list.append(ccs.atoms.copy()) if visual: ase.view(list)
def test_var_mask_big_water_opt(self): print "Running optimisation tests with masking of variables:" print " Lots of water molecules with internal and rotational coordinates frozen." ccs, var_types = self.form_ccs_waters(16) def gen_mask(c): if c == 'i': return False elif c == 'r': return True elif c == 'p': return True else: raise False mask = map(gen_mask, var_types) ccs.set_var_mask(mask) ccs.set_calculator(test_calc()) opt = ase.LBFGS(ccs) list = [] for i in range(8): opt.run(steps=1,fmax=0.0) list.append(ccs.atoms.copy()) if visual: ase.view(list)
def test_ComplexCoordSys(self): x = cs.XYZ(file2str("H2.xyz")) a_h2o1 = cs.RotAndTrans(numpy.array([1.,0.,0.,3.,1.,1.]), parent=x) a_h2o2 = cs.RotAndTrans(numpy.array([1.,0.,0.,1.,1.,1.]), parent=x) a_ch4 = cs.RotAndTrans(numpy.array([1.,0.,0.,1.,-1.,1.]), parent=x) h2o1 = cs.ZMatrix(file2str("H2O.zmt"), anchor=a_h2o1) h2o2 = cs.ZMatrix(file2str("H2O.zmt"), anchor=a_h2o2) ch4 = cs.ZMatrix(file2str("CH4.zmt"), anchor=a_ch4) parts = [x, h2o1, h2o2, ch4] ccs = cs.ComplexCoordSys(parts) ccs.set_calculator(test_calc()) dyn = ase.LBFGS(ccs) list = [] for i in range(8): list.append(ccs.atoms.copy()) dyn.run(steps=1,fmax=0.01) list.append(ccs.atoms.copy()) if visual: ase.view(list)
def test_Anchoring(self): #self.assertRaises(cs.ComplexCoordSysException, cs.RotAndTrans, numpy.array([0.,0.,0.,0.,0.,0.])) # Generates a series of rotated molecular geometries and views them. a = cs.RotAndTrans(numpy.array([1.,0.,0.,1.,1.,1.])) z = cs.ZMatrix(file2str("butane1.zmt"), anchor=a) print z.get_internals() atoms1 = z.atoms.copy() alphas = numpy.arange(0., 1., 0.01) * 2 * numpy.pi vs = numpy.arange(0., 1., 0.01) geoms_list = [] for alpha, v in zip(alphas, vs): vec = common.normalise([1,v,-v]) vec = numpy.sin(alpha / 2) * vec q = numpy.hstack([vec]) v = numpy.array([1.,1.,1.]) a.set(numpy.hstack([q,v])) geoms_list.append(z.atoms.copy()) if visual: ase.view(geoms_list)
def setUp(self): UTDomainParallelSetup.setUp(self) for virtvar in ['dtype']: assert getattr(self,virtvar) is not None, 'Virtual "%s"!' % virtvar # Create randomized atoms self.atoms = create_random_atoms(self.gd, 5) # also tested: 10xNH3/BDA # XXX DEBUG START if False: from ase import view view(self.atoms*(1+2*self.gd.pbc_c)) # XXX DEBUG END # Do we agree on the atomic positions? pos_ac = self.atoms.get_positions() pos_rac = np.empty((world.size,)+pos_ac.shape, pos_ac.dtype) world.all_gather(pos_ac, pos_rac) if (pos_rac-pos_rac[world.rank,...][np.newaxis,...]).any(): raise RuntimeError('Discrepancy in atomic positions detected.') # Create setups for atoms self.Z_a = self.atoms.get_atomic_numbers() self.setups = Setups(self.Z_a, p.setups, p.basis, p.lmax, xc) # K-point descriptor bzk_kc = np.array([[0, 0, 0]], dtype=float) self.kd = KPointDescriptor(bzk_kc, 1) self.kd.set_symmetry(self.atoms, self.setups, usesymm=True) self.kd.set_communicator(self.kpt_comm) # Create gamma-point dummy wavefunctions self.wfs = FDWFS(self.gd, self.bd, self.kd, self.setups, self.dtype) spos_ac = self.atoms.get_scaled_positions() % 1.0 self.wfs.set_positions(spos_ac) self.pt = self.wfs.pt # XXX shortcut ## Also create pseudo partial waveves #from gpaw.lfc import LFC #self.phit = LFC(self.gd, [setup.phit_j for setup in self.setups], \ # self.kpt_comm, dtype=self.dtype) #self.phit.set_positions(spos_ac) self.r_cG = None self.buf_G = None self.psit_nG = None self.allocate()
def setUp(self): UTDomainParallelSetup.setUp(self) for virtvar in ['dtype']: assert getattr(self,virtvar) is not None, 'Virtual "%s"!' % virtvar # Create randomized atoms self.atoms = create_random_atoms(self.gd, 5) # also tested: 10xNH3/BDA # XXX DEBUG START if False: from ase import view view(self.atoms*(1+2*self.gd.pbc_c)) # XXX DEBUG END # Do we agree on the atomic positions? pos_ac = self.atoms.get_positions() pos_rac = np.empty((world.size,)+pos_ac.shape, pos_ac.dtype) world.all_gather(pos_ac, pos_rac) if (pos_rac-pos_rac[world.rank,...][np.newaxis,...]).any(): raise RuntimeError('Discrepancy in atomic positions detected.') # Create setups for atoms self.Z_a = self.atoms.get_atomic_numbers() self.setups = Setups(self.Z_a, p.setups, p.basis, p.lmax, xc) # K-point descriptor bzk_kc = np.array([[0, 0, 0]], dtype=float) self.kd = KPointDescriptor(bzk_kc, 1) self.kd.set_symmetry(self.atoms, self.setups) self.kd.set_communicator(self.kpt_comm) # Create gamma-point dummy wavefunctions self.wfs = FDWFS(self.gd, self.bd, self.kd, self.setups, self.block_comm, self.dtype) spos_ac = self.atoms.get_scaled_positions() % 1.0 self.wfs.set_positions(spos_ac) self.pt = self.wfs.pt # XXX shortcut ## Also create pseudo partial waveves #from gpaw.lfc import LFC #self.phit = LFC(self.gd, [setup.phit_j for setup in self.setups], \ # self.kpt_comm, dtype=self.dtype) #self.phit.set_positions(spos_ac) self.r_cG = None self.buf_G = None self.psit_nG = None self.allocate()
def beadopt_calc(mi, params, indices): """Check that points with the specified indices are minima and minimise if necessary.""" for i in indices: mol = mi.build_coord_sys(mi.reagent_coords[i]) opt = ase.LBFGS(mol) s, l = 0, [] # FIXME: the final force norm printed by the optimiser is greater than the one specified here maxit = params['maxit'] tol = params['tol'] while numpy.max(mol.get_forces()) > tol: opt.run(steps=1) s += 1 l.append(mol.atoms.copy()) if s >= maxit: print "Max iterations exceeded." break ase.view(l)
def test_ComplexCoordSys_var_mask_opt(self): print "Running tests with masking of variables" #ch4 = cs.ZMatrix(file2str("CH4.zmt")) ccs, x, h2o1, a_h2o1 = self.form_ccs1( ) #, h2o2, ch4, a_h2o2, a_ch4 = self.form_ccs() ccs.set_calculator(test_calc()) #m = [True for i in range(0)] + [True for i in range(ccs.dims)] parts = [x, h2o1, a_h2o1] #, h2o2, a_h2o2, ch4, a_ch4] dims = [p._dims for p in parts] print dims torf = lambda d, f: [f for i in range(d)] fs = [False, False, True] #, True, False, False, False] m1 = [torf(d, f) for d, f in zip(dims, fs)] print m1 m = [m_ for d, f in zip(dims, fs) for m_ in torf(d, f)] print m mask = numpy.array(m) ccs.set_var_mask(mask) self.assert_(sum(ccs._var_mask) == ccs.dims) self.assertAlmostEqualVec(ccs._coords, ccs._demask(ccs.get_internals())) before = ccs._coords.copy() #print ccs.int2cart(ccs.get_internals()) after = ccs._coords.copy() self.assert_((before == after).all()) print "_coords", ccs._coords if visual: ase.view(ccs.atoms) print "_coords", ccs._coords print ccs.get_internals() if visual: ase.view(ccs.atoms) dyn = ase.LBFGS(ccs) list = [] for i in range(8): list.append(ccs.atoms.copy()) dyn.run(steps=1, fmax=0.01) list.append(ccs.atoms.copy()) #if visual: ase.view(list)
def test_ComplexCoordSys2(self): x = cs.XYZ(file2str("H2.xyz")) a = cs.RotAndTrans(numpy.array([1., 0., 0., 3., 1., 1.]), parent=x) z = cs.ZMatrix(file2str("butane1.zmt"), anchor=a) parts = [x, z] ccs = cs.ComplexCoordSys(parts) ccs.set_calculator(test_calc()) print ccs.get_potential_energy() print ccs.get_forces() dyn = ase.LBFGS(ccs) list = [] for i in range(8): dyn.run(steps=1, fmax=0.01) list.append(ccs.atoms.copy()) if visual: ase.view(list)
def test_ComplexCoordSys2(self): x = cs.XYZ(file2str("H2.xyz")) a = cs.RotAndTrans(numpy.array([1.,0.,0.,3.,1.,1.]), parent=x) z = cs.ZMatrix(file2str("butane1.zmt"), anchor=a) parts = [x, z] ccs = cs.ComplexCoordSys(parts) ccs.set_calculator(test_calc()) print ccs.get_potential_energy() print ccs.get_forces() dyn = ase.LBFGS(ccs) list = [] for i in range(8): dyn.run(steps=1,fmax=0.01) list.append(ccs.atoms.copy()) if visual: ase.view(list)
def test_ComplexCoordSys_var_mask_opt(self): print "Running tests with masking of variables" #ch4 = cs.ZMatrix(file2str("CH4.zmt")) ccs, x, h2o1, a_h2o1 = self.form_ccs1()#, h2o2, ch4, a_h2o2, a_ch4 = self.form_ccs() ccs.set_calculator(test_calc()) #m = [True for i in range(0)] + [True for i in range(ccs.dims)] parts = [x, h2o1, a_h2o1]#, h2o2, a_h2o2, ch4, a_ch4] dims = [p._dims for p in parts] print dims torf = lambda d, f: [f for i in range(d)] fs = [False, False, True]#, True, False, False, False] m1 = [torf(d,f) for d,f in zip(dims, fs)] print m1 m = [m_ for d,f in zip(dims, fs) for m_ in torf(d,f)] print m mask = numpy.array(m) ccs.set_var_mask(mask) self.assert_(sum(ccs._var_mask) == ccs.dims) self.assertAlmostEqualVec(ccs._coords, ccs._demask(ccs.get_internals())) before = ccs._coords.copy() #print ccs.int2cart(ccs.get_internals()) after = ccs._coords.copy() self.assert_((before == after).all()) print "_coords", ccs._coords if visual: ase.view(ccs.atoms) print "_coords", ccs._coords print ccs.get_internals() if visual: ase.view(ccs.atoms) dyn = ase.LBFGS(ccs) list = [] for i in range(8): list.append(ccs.atoms.copy()) dyn.run(steps=1,fmax=0.01) list.append(ccs.atoms.copy()) #if visual: ase.view(list)
os.path.join(path, '2387', 'reactant.con'), {1:{1:3.9,2:3.7},2:{1:3.7,2:3.3}}) chn2 = ChangingNeighbors(os.path.join(path, '0', 'reactant.con'), os.path.join(path, '1635', 'reactant.con'), {1:{1:3.9,2:3.7},2:{1:3.7,2:3.3}}) chn3 = ChangingNeighbors(os.path.join(path, '1635', 'reactant.con'), os.path.join(path, '2387', 'reactant.con'), {1:{1:3.9,2:3.7},2:{1:3.7,2:3.3}}) reac1 = chn1.neigh1.atoms t1 = chn1.nrValues(chn1.lostNeighbors) t2 = chn1.nrValues(chn2.lostNeighbors) t3 = chn1.nrValues(chn3.lostNeighbors) indx1 = (t1 > 0) indx2 = (t2 > 0) indx3 = (t3 > 0) aseAtoms = cc.eon_2_ase(reac1) aseAtoms = ca.color(aseAtoms,indexes=indx1,color='red', makeCopy=False) aseAtoms = ca.color(aseAtoms,indexes=indx2,color='green', makeCopy=False) aseAtoms = ca.color(aseAtoms,indexes=indx3,color='blue', makeCopy=False) aseAtoms = ca.color(aseAtoms,indexes=np.logical_and(indx2,indx3),color='yellow', makeCopy=False) view(aseAtoms)
def run(order_str, fn1, fn2, ixs): """ Setup everything and run minimisation. Based on ordering given in order_Str, geometries in fn1 and fn2, and the indices ixs, rotate molecule in fn2 so that the atoms with indices ixs coincide with those in fn1. """ # get re-ordering list order = order_str.split() order = array([int(o) for o in order]) tot = len(order) assert tot % 2 == 0, "Atom ordering given has odd number of indices." order = order.reshape(-1,2) order1 = order[:,0].copy() order2 = order[:,1].copy() order1.sort(), order2.sort() assert (order1 == order2).all(), "Atom ordering given has non-matching numbers." a1 = ase.io.read(fn1) a2 = ase.io.read(fn2) assert len(a1) == len(a2), "Molecules specified don't have the same number of atoms." n = len(a1) assert len(order) == len(a1), "Number of atoms was %d but number of pairs given was %d." % (len(a1), len(order)) # get coords geom1 = a1.get_positions().copy() geom2 = a2.get_positions().copy() # re-order coords so that they are both the same g1 = [] g2 = [] chem_symbols = [] for o in order: i,j = o g1.append(geom1[i]) g2.append(geom2[j]) print i, j, a1.get_chemical_symbols()[i] + " = " + a2.get_chemical_symbols()[j] assert a1.get_chemical_symbols()[i] == a2.get_chemical_symbols()[j], a1.get_chemical_symbols()[i] + " = " + a2.get_chemical_symbols()[j] chem_symbols.append(a2.get_chemical_symbols()[j]) g1 = array(g1) g2 = array(g2) old_dist_sum1 = aaa_dist(g1) old_dist_sum2 = aaa_dist(g2) a1.set_positions(g1) r = Rotate(g1.copy(), g2.copy(), ixs=ixs) x, err, g2_new, _ = r.align() print "Optimising vector:", x print "Alignment error:", err g2_new = r.trans(g2, x) a1.set_chemical_symbols(chem_symbols) a2.set_chemical_symbols(chem_symbols) a1.set_positions(g1) a2.set_positions(g2_new) new_dist_sum1 = aaa_dist(g1) new_dist_sum2 = aaa_dist(g2_new) # Make sure the geometries haven't changes shape in any way by comparing # the old/new interatom distance. assert abs(old_dist_sum1 - new_dist_sum1) < 1e-6 assert abs(old_dist_sum2 - new_dist_sum2) < 1e-6 ase.view([a1,a2]) ase.io.write(fn1 + '.t', a1, format='xyz') ase.io.write(fn2 + '.t', a2, format='xyz')
logo = """\ H HH HHH H H H H HHH H HH H H H H H H HH HHH""" d = 0.8 atoms = Atoms() for y, line in enumerate(logo.split('\n')): for x, c in enumerate(line): if c == 'H': atoms.append(Atom('H', [d * x, -d * y, 0])) atoms.center(vacuum=2.0) view(atoms) if 0: calc = GPAW(nbands=30) atoms.set_calculator(calc) atoms.get_potential_energy() calc.write('ase-logo.gpw') else: calc = GPAW('ase-logo.gpw', txt=None) density = calc.get_pseudo_density() image = density[..., density.shape[2] // 2] if 1: # scale colors to wiki background / foreground import numpy as np background = np.array([[[19., 63., 82.]]]).T / 255 # 1c4e63 blueish