def test_complex_array(self): d = Dictionary() d['complex'] = fzeros(10, dtype='D') a = FortranArray(d.get_array('complex')) a[:] = 1 + 2j self.assertEqual(a.dtype.kind, 'c') self.assertArrayAlmostEqual(a, d['complex'])
def testinitstring(self): params = Dictionary("a=1 b=2 c=3") self.assertEqual(params['a'], 1) self.assertEqual(params['b'], 2) self.assertEqual(params['c'], 3) self.assertEqual(params.keys(), ["a", "b", "c"]) self.assertEqual(params.values(), [1, 2, 3])
def test_dict_in_dict(self): d = Dictionary() d2 = Dictionary() d2['a'] = 1 d['d2'] = d2 print d print d['d2']
def testcopy(self): params = Dictionary('a="one two" b="2.943"') params2 = params.copy() from copy import copy params3 = copy(params) self.assertEqual(params.keys(), params2.keys()) self.assertEqual(params.values(), params2.values()) self.assertEqual(params.values(), params3.values())
def testwrite(self): params = Dictionary('a="one two" b="2.943" c=44') from StringIO import StringIO s = StringIO() params.write(s) self.assertEqual(s.getvalue(), """a="one two" b=2.943 c=44""")
def test_real_array2(self): d = Dictionary() d.add_array('real', 0.0, (3, 3)) d_real = FortranArray(d.get_array('real')) d_real[1, 1] = 3. d_real[2, 2] = 1. d_real[:, 3] = (4., 1., 5.) self.assertArrayAlmostEqual(d_real, d['real'])
def test_int_array2(self): d = Dictionary() d.add_array('int', 0, (3, 3)) d_int = FortranArray(d.get_array('int')) d_int[1, 1] = 3 d_int[2, 2] = 1 d_int[:, 3] = (4, 1, 5) self.assert_(all(d_int == d['int']))
def testinitdictionary(self): p1 = Dictionary("a=1 b=2 c=3") p2 = Dictionary(p1) self.assertEqual(p2['a'], 1) self.assertEqual(p2['b'], 2) self.assertEqual(p2['c'], 3) self.assertEqual(p2.keys(), ["a", "b", "c"]) self.assertEqual(p2.values(), [1, 2, 3])
def test_deepcopy(self): d1 = Dictionary() d1['ia2'] = [[1, 2, 3], [4, 5, 6]] d2 = Dictionary() d2.deepcopy(d1) # copy from d1 into d2 self.assert_(all(d1['ia2'] == d2['ia2'])) d1['ia2'][1, 1] = 0 self.assert_(not all(d1['ia2'] == d2['ia2'])) self.assert_(not all(d1._fpointer == d2._fpointer))
def test_logical_array(self): d = Dictionary() d['logical'] = fzeros(5, dtype='bool') a = FortranArray(d.get_array('logical')) a[:] = [True, False, False, True, True] self.assertEqual( a.dtype, dtype('int32')) # Fortran logical represented as int32 internally self.assertArrayAlmostEqual(a, d['logical'])
def test_subset(self): d = Dictionary() d['i'] = 1 d['s'] = 'string' d['ia'] = [1, 2, 3] d['sa'] = ['string', 'longer string'] d['la'] = [True, False] d['ia2'] = [[1, 2, 3], [4, 5, 6]] d2 = d.subset(['i', 'ia', 'la']) self.assert_(d2['i'] == 1 and list(d2['ia']) == [1, 2, 3] and list(d2['la']) == [True, False])
def test_get_value(self): d = Dictionary() d['i'] = 1 d['s'] = 'string' d['ia'] = [1, 2, 3] d['sa'] = ['string', 'longer string'] d['la'] = [True, False] d['ia2'] = [[1, 2, 3], [4, 5, 6]] for k in d: v1 = d[k] v2 = d.get_value(k) if hasattr(v1, '__iter__'): self.assert_(all(v1 == v2)) else: self.assertEqual(v1, v2)
def testquotedstring(self): params = Dictionary('a="one two" b="2.943"') self.assertEqual(params.keys(), ["a", "b"]) self.assertEqual(params['a'], "one two") self.assertAlmostEqual(params['b'], 2.943)
def testinitlines(self): lines = ['a=1\n', 'b=2\n'] p = Dictionary(lines) self.assertEqual(p.keys(), ['a', 'b']) self.assertEqual(p.values(), [1, 2])
def testinitdict(self): params = Dictionary({'a': 1, 'b': 2}) self.assertEqual(sorted(params.keys()), ['a', 'b'])
def test_real_array(self): d = Dictionary() d['real'] = [float(i) for i in range(10)] self.assertArrayAlmostEqual(FortranArray(d.get_array('real')), d['real'])
def test_int_array(self): d = Dictionary() d['int'] = [i for i in range(10)] self.assertArrayAlmostEqual(FortranArray(d.get_array('int')), d['int'])
try: xy_ring = np.loadtxt('xy_ring.csv', dtype='int') temp = set(xy_ring) for idx in xy_ring: ring_indices = np.where(((crack_slab.positions[:,:2] - crack_slab.positions[idx,:2])**2).sum(axis=1)**0.5 < 0.5)[0] temp = temp.union(set(ring_indices)) xy_ring = temp except: print("WARNING: Breaking ring on XY plane not specified, using very small QM buffers") xy_ring = set() hybrid_mark = np.array([1 if i in set(core_ring).union(set(xy_ring)) else 0 for i in range(len(crack_slab))]) crack_slab.set_array('hybrid_mark', hybrid_mark) # CALC_ARGS: use first to have radius cutoff, use second for a bond hop construction # calc_args = Dictionary('little_clusters=F terminate even_electrons cluster_vacuum=12.0 cluster_calc_connect=F buffer_hops=1 transition_hops=0 randomise_buffer=F hysteretic_connect=F nneighb_only cluster_hopping_nneighb_only property_list=species:pos:hybrid_mark:index cluster_box_buffer=20.0 cluster_hopping=F keep_whole_residues=F min_images_only keep_whole_silica_tetrahedra protect_double_bonds=F force_no_fix_termination_clash=F termination_clash_factor=1.8 nneighb_different_z in_out_in=F cluster_hopping_skip_unreachable hysteretic_buffer=T hysteretic_buffer_inner_radius=7.0 hysteretic_buffer_outer_radius=9.0') calc_args = Dictionary('little_clusters=T terminate even_electrons cluster_vacuum=12.0 cluster_calc_connect=F buffer_hops=%d transition_hops=0 randomise_buffer=F hysteretic_connect=F nneighb_only cluster_hopping_nneighb_only property_list=species:pos:hybrid_mark:index cluster_box_buffer=20.0 cluster_hopping=T keep_whole_residues=F min_images_only keep_whole_silica_tetrahedra protect_double_bonds=F force_no_fix_termination_clash=F termination_clash_factor=1.8 nneighb_different_z in_out_in=F cluster_hopping_skip_unreachable hysteretic_buffer=F hysteretic_buffer_inner_radius=7.0 hysteretic_buffer_outer_radius=9.0 cluster_same_lattice=T' % buffer_hops) pretty = quippy.Atoms(crack_slab) pretty.set_pbc([True]*3) pretty.calc_connect() cluster_args = calc_args.copy() create_hybrid_weights_args = calc_args.copy() if create_hybrid_weights_args['buffer_hops'] == 0: create_hybrid_weights_args['buffer_hops'] = 1 # FIXME disable shortcut create_hybrid_weights_args_str = quippy.util.args_str(create_hybrid_weights_args) create_hybrid_weights(pretty, args_str=create_hybrid_weights_args_str) cluster = create_cluster_simple(pretty, args_str=quippy.util.args_str(cluster_args)) cluster_indices = np.array(cluster.orig_index - 1) fix_in_dft = np.array([i for i in cluster.indices if (cluster.hybrid_mark[i] not in [1,2])]) cluster_ase = ase.Atoms(np.array(cluster.z), np.array(cluster.positions)) shift_cluster = cluster_ase.get_positions().min(axis=0) + 0.5 * cluster_vacuum cluster_ase.positions -= shift_cluster
def testvaluelists(self): params = Dictionary('xyz={1.0 2.0 3.0} abc="1 2 3"') self.assertAlmostEqual(params['xyz'][1], 1.0) self.assertAlmostEqual(params['xyz'][2], 2.0) self.assertAlmostEqual(params['xyz'][3], 3.0) self.assertArrayAlmostEqual(params['abc'], farray([1, 2, 3]))
def test_string_array(self): d = Dictionary() d['string'] = ['one', 'two', 'three'] a = FortranArray(d.get_array('string')) a[:, 1] = farray(list('hello'), 'S1') self.assertEqual(list(a2s(d['string'])), list(a2s(a)))
def testrepr(self): params = Dictionary('a="one two" b="2.943"') self.assertEqual(repr(params), """Dictionary('a="one two" b=2.943')""")
def test_subset_bad_key(self): d = Dictionary() self.assertRaises(RuntimeError, d.subset, ['bad_key'])
def testasstring(self): params = Dictionary('a="one two" b="2.943" c="44 45"') from StringIO import StringIO s = StringIO() s.write(params.asstring(' ')) self.assertEqual(s.getvalue(), 'a="one two" b=2.943 c="44 45"')
def test_bcast(self): from quippy import MPI_context, bcast d = Dictionary() mpi = MPI_context() bcast(mpi, d)
def __init__(self, symbols=None, positions=None, numbers=None, tags=None, momenta=None, masses=None, magmoms=None, charges=None, scaled_positions=None, cell=None, pbc=None, constraint=None, calculator=None, info=None, n=None, lattice=None, properties=None, params=None, fixed_size=None, set_species=True, fpointer=None, finalise=True, **readargs): # check for mutually exclusive options if cell is not None and lattice is not None: raise ValueError('only one of cell and lattice can be present') if n is None: n = 0 if cell is not None: lattice = cell.T if lattice is None: lattice = np.eye(3) from quippy import Dictionary if properties is not None and not isinstance(properties, Dictionary): properties = Dictionary(properties) if params is not None and not isinstance(params, Dictionary): params = Dictionary(params) _atoms.Atoms.__init__(self, n=n, lattice=lattice, properties=properties, params=params, fixed_size=fixed_size, fpointer=fpointer, finalise=finalise) self._ase_arrays = PropertiesWrapper(self) # If first argument is quippy.Atoms instance, copy data from it if isinstance(symbols, self.__class__): self.copy_from(symbols) symbols = None # Phonopy compatibility if 'phonopy' in available_modules: if symbols is not None and isinstance(symbols, PhonopyAtoms): atoms = symbols symbols = atoms.get_chemical_symbols() cell = atoms.get_cell() positions = atoms.get_positions() masses = atoms.get_masses() # Try to read from first argument, if it's not ase.Atoms if symbols is not None and not isinstance(symbols, ase.Atoms): self.read_from(symbols, **readargs) symbols = None ## ASE compatibility remove_properties = [] if symbols is None and numbers is None: if self.has_property('z'): numbers = self.z.view(np.ndarray) else: numbers = [0] * len(self) remove_properties.append('Z') if symbols is None and positions is None: if self.has_property('pos'): positions = self.pos.view(np.ndarray).T else: remove_properties.append('pos') # Make sure argument to ase.Atoms constructor are consistent with # properties already present in this Atoms object if symbols is None and momenta is None and self.has_property( 'momenta'): momenta = self.get_momenta() if symbols is None and masses is None and self.has_property('masses'): masses = self.get_masses() if symbols is None and cell is None: cell = self.lattice.T.view(np.ndarray) if symbols is None and pbc is None: pbc = self.get_pbc() if charges is None and self.has_property('charge'): charges = self.charge.view(np.ndarray) ase.Atoms.__init__(self, symbols, positions, numbers, tags, momenta, masses, magmoms, charges, scaled_positions, cell, pbc, constraint, calculator) # remove anything that ASE added that we don't want for p in remove_properties: self.remove_property(p) if isinstance(symbols, ase.Atoms): self.copy_from(symbols) ## end ASE compatibility if set_species and self.has_property('Z'): if not self.has_property('species'): self.add_property('species', ' ' * TABLE_STRING_LENGTH) if self.n != 0 and not (self.z == 0).all(): self.set_atoms(self.z) # initialise species from z if info is not None: self.params.update(info) self._initialised = True # synonyms for backwards compatibility self.neighbours = self.connect self.hysteretic_neighbours = self.hysteretic_connect
def test_none_value(self): d = Dictionary('a=1 b=two c') self.assert_(d['a'] == 1) self.assert_(d['b'] == 'two') self.assert_(d['c'] is None)