def write(self, at, **kwargs): import ase.db from ase.calculators.singlepoint import SinglePointCalculator all_kwargs = self.kwargs.copy() all_kwargs.update(kwargs) all_kwargs.update(at.params) energy = at.params.get('energy', None) forces = getattr(at, 'force', None) if forces is not None: forces = forces.T stress = at.params.get('virial', None) if stress is not None: stress = -stress.view(np.ndarray) / at.get_volume() orig_calc = at.get_calculator() params = {} data = {} skip_params = ['energy', 'virial', 'calculator', 'id', 'unique_id'] # filter out duplicate data for (key, value) in all_kwargs.items(): key = key.lower() if key in skip_params: continue if (isinstance(value, int) or isinstance(value, basestring) or isinstance(value, float) or isinstance(value, bool)): # scalar key/value pairs params[key] = value else: # more complicated data structures data[key] = value skip_arrays = ['numbers', 'positions', 'species'] for (key, value) in at.arrays.items(): if key in skip_arrays: continue key = key.lower() data[key] = value try: calc = SinglePointCalculator(atoms=at, energy=energy, forces=forces, stress=stress) if orig_calc is not None: calc.name = orig_calc.name else: calc.name = all_kwargs.get('calculator', '(unknown)') at.set_calculator(calc) database = ase.db.connect(self.dbfile) database.write(at, key_value_pairs=params, data=data) finally: at.set_calculator(orig_calc)
def __getitem__(self, i=-1): if isinstance(i, slice): return SlicedTrajectory(self, i) b = self.backend[i] if 'numbers' in b: # numbers and other header info was written alongside the image: atoms = read_atoms(b, traj=self) else: # header info was not written because they are the same: atoms = read_atoms( b, header=[self.pbc, self.numbers, self.masses, self.constraints], traj=self) if 'calculator' in b: results = {} implemented_properties = [] c = b.calculator for prop in all_properties: if prop in c: results[prop] = c.get(prop) implemented_properties.append(prop) calc = SinglePointCalculator(atoms, **results) calc.name = b.calculator.name calc.implemented_properties = implemented_properties if 'parameters' in c: calc.parameters.update(c.parameters) atoms.calc = calc return atoms
def __getitem__(self, i=-1): b = self.backend[i] atoms = Atoms(positions=b.positions, numbers=self.numbers, cell=b.cell, masses=self.masses, pbc=self.pbc, celldisp=self.celldisp, info=b.get('info'), constraint=[dict2constraint(d) for d in decode(self.constraints)], momenta=b.get('momenta'), magmoms=b.get('magmoms'), charges=b.get('charges'), tags=b.get('tags')) atoms._readTags(self.new_tags) if 'calculator' in b: results = {} c = b.calculator for prop in all_properties: if prop in c: results[prop] = c.get(prop) calc = SinglePointCalculator(atoms, **results) calc.name = b.calculator.name atoms.set_calculator(calc) return atoms
def __getitem__(self, i=-1): b = self.backend[i] atoms = read_atoms(b, header=[self.pbc, self.numbers, self.masses, self.constraints]) if 'calculator' in b: results = {} c = b.calculator for prop in all_properties: if prop in c: results[prop] = c.get(prop) calc = SinglePointCalculator(atoms, **results) calc.name = b.calculator.name atoms.set_calculator(calc) return atoms
def __getitem__(self, i=-1): b = self.backend[i] if 'numbers' in b: # numbers and other header info was written alongside the image: atoms = read_atoms(b) else: # header info was not written because they are the same: atoms = read_atoms(b, header=[self.pbc, self.numbers, self.masses, self.constraints]) if 'calculator' in b: results = {} c = b.calculator for prop in all_properties: if prop in c: results[prop] = c.get(prop) calc = SinglePointCalculator(atoms, **results) calc.name = b.calculator.name atoms.set_calculator(calc) return atoms
def __getitem__(self, i=-1): b = self.backend[i] if 'numbers' in b: # numbers and other header info was written alongside the image: atoms = read_atoms(b) else: # header info was not written because they are the same: atoms = read_atoms(b, header=[self.pbc, self.numbers, self.masses, self.constraints]) if 'calculator' in b: results = {} c = b.calculator for prop in all_properties: if prop in c: results[prop] = c.get(prop) calc = SinglePointCalculator(atoms, **results) calc.name = b.calculator.name atoms.set_calculator(calc) return atoms
def __getitem__(self, i=-1): b = self.backend[i] atoms = Atoms( positions=b.positions, numbers=self.numbers, cell=b.cell, masses=self.masses, pbc=self.pbc, info=b.get('info'), constraint=[dict2constraint(d) for d in decode(self.constraints)], momenta=b.get('momenta'), magmoms=b.get('magmoms'), charges=b.get('charges'), tags=b.get('tags')) if 'calculator' in b: results = {} c = b.calculator for prop in all_properties: if prop in c: results[prop] = c.get(prop) calc = SinglePointCalculator(atoms, **results) calc.name = b.calculator.name atoms.set_calculator(calc) return atoms