def view(atoms, data=None, viewer='ag', repeat=None, block=False): # Ignore for parallel calculations: if parallel.size != 1: return if hasattr(atoms, 'GetUnitCell'): # Convert old ASE ListOfAtoms to new style. atoms = OldASEListOfAtomsWrapper(atoms).copy() vwr = viewer.lower() if vwr == 'ag': format = 'traj' if repeat is None: command = 'ag' else: command = 'ag --repeat=%d,%d,%d' % tuple(repeat) repeat = None elif vwr == 'vmd': format = 'cube' command = 'vmd' elif vwr == 'rasmol': format = 'pdb' command = 'rasmol -pdb' elif vwr == 'xmakemol': format = 'xyz' command = 'xmakemol -f' elif vwr == 'gopenmol': format = 'xyz' command = 'rungOpenMol' elif vwr == 'avogadro': format = 'cube' command = 'avogadro' elif vwr == 'sage': from ase.visualize.sage import view_sage_jmol view_sage_jmol(atoms) return else: raise RuntimeError('Unknown viewer: ' + viewer) fd, filename = tempfile.mkstemp('.' + format, 'ase-') fd = os.fdopen(fd, 'w') if repeat is not None: atoms = atoms.repeat() if data is None: write(fd, atoms, format=format) else: write(fd, atoms, format=format, data=data) fd.close() if block: os.system('%s %s' % (command, filename)) os.remove(filename) else: if os.name in ['ce', 'nt']: # Win # XXX: how to make it non-blocking? os.system('%s %s' % (command, filename)) os.remove(filename) else: os.system('%s %s & ' % (command, filename)) os.system('(sleep 60; rm %s) &' % filename)
def get_atoms(self): atoms = OldASEListOfAtomsWrapper(self.loa).copy() atoms.set_calculator(self) return atoms
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): atoms = None if hasattr(symbols, 'GetUnitCell'): from ase.old import OldASEListOfAtomsWrapper atoms = OldASEListOfAtomsWrapper(symbols) symbols = None elif hasattr(symbols, "get_positions"): atoms = symbols symbols = None elif (isinstance(symbols, (list, tuple)) and len(symbols) > 0 and isinstance(symbols[0], Atom)): # Get data from a list or tuple of Atom objects: data = zip(*[atom.get_data() for atom in symbols]) atoms = Atoms(None, *data) symbols = None if atoms is not None: # Get data from another Atoms object: if scaled_positions is not None: raise NotImplementedError if symbols is None and numbers is None: numbers = atoms.get_atomic_numbers() if positions is None: positions = atoms.get_positions() if tags is None and atoms.has('tags'): tags = atoms.get_tags() if momenta is None and atoms.has('momenta'): momenta = atoms.get_momenta() if magmoms is None and atoms.has('magmoms'): magmoms = atoms.get_initial_magnetic_moments() if masses is None and atoms.has('masses'): masses = atoms.get_masses() if charges is None and atoms.has('charges'): charges = atoms.get_charges() if cell is None: cell = atoms.get_cell() if pbc is None: pbc = atoms.get_pbc() if constraint is None: constraint = [c.copy() for c in atoms.constraints] self.arrays = {} if symbols is None: if numbers is None: if positions is not None: natoms = len(positions) elif scaled_positions is not None: natoms = len(scaled_positions) else: natoms = 0 numbers = np.zeros(natoms, int) self.new_array('numbers', numbers, int) else: if numbers is not None: raise ValueError( 'Use only one of "symbols" and "numbers".') else: self.new_array('numbers', symbols2numbers(symbols), int) if cell is None: cell = np.eye(3) self.set_cell(cell) if positions is None: if scaled_positions is None: positions = np.zeros((len(self.arrays['numbers']), 3)) else: positions = np.dot(scaled_positions, self._cell) else: if scaled_positions is not None: raise RuntimeError, 'Both scaled and cartesian positions set!' self.new_array('positions', positions, float, (3,)) self.set_constraint(constraint) self.set_tags(default(tags, 0)) self.set_momenta(default(momenta, (0.0, 0.0, 0.0))) self.set_masses(default(masses, None)) self.set_initial_magnetic_moments(default(magmoms, 0.0)) self.set_charges(default(charges, 0.0)) if pbc is None: pbc = False self.set_pbc(pbc) self.adsorbate_info = {} self.set_calculator(calculator)
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): atoms = None if hasattr(symbols, 'GetUnitCell'): from ase.old import OldASEListOfAtomsWrapper atoms = OldASEListOfAtomsWrapper(symbols) symbols = None elif hasattr(symbols, 'get_positions'): atoms = symbols symbols = None elif (isinstance(symbols, (list, tuple)) and len(symbols) > 0 and isinstance(symbols[0], Atom)): # Get data from a list or tuple of Atom objects: data = zip(*[atom.get_data() for atom in symbols]) atoms = self.__class__(None, *data) symbols = None if atoms is not None: # Get data from another Atoms object: if scaled_positions is not None: raise NotImplementedError if symbols is None and numbers is None: numbers = atoms.get_atomic_numbers() if positions is None: positions = atoms.get_positions() if tags is None and atoms.has('tags'): tags = atoms.get_tags() if momenta is None and atoms.has('momenta'): momenta = atoms.get_momenta() if magmoms is None and atoms.has('magmoms'): magmoms = atoms.get_initial_magnetic_moments() if masses is None and atoms.has('masses'): masses = atoms.get_masses() if charges is None and atoms.has('charges'): charges = atoms.get_charges() if cell is None: cell = atoms.get_cell() if pbc is None: pbc = atoms.get_pbc() if constraint is None: constraint = [c.copy() for c in atoms.constraints] if calculator is None: calculator = atoms.get_calculator() self.arrays = {} if symbols is None: if numbers is None: if positions is not None: natoms = len(positions) elif scaled_positions is not None: natoms = len(scaled_positions) else: natoms = 0 numbers = np.zeros(natoms, int) self.new_array('numbers', numbers, int) else: if numbers is not None: raise ValueError('Use only one of "symbols" and "numbers".') else: self.new_array('numbers', symbols2numbers(symbols), int) if cell is None: cell = np.eye(3) self.set_cell(cell) if positions is None: if scaled_positions is None: positions = np.zeros((len(self.arrays['numbers']), 3)) else: positions = np.dot(scaled_positions, self._cell) else: if scaled_positions is not None: raise RuntimeError, 'Both scaled and cartesian positions set!' self.new_array('positions', positions, float, (3, )) self.set_constraint(constraint) self.set_tags(default(tags, 0)) self.set_momenta(default(momenta, (0.0, 0.0, 0.0))) self.set_masses(default(masses, None)) self.set_initial_magnetic_moments(default(magmoms, 0.0)) self.set_charges(default(charges, 0.0)) if pbc is None: pbc = False self.set_pbc(pbc) self.adsorbate_info = {} self.set_calculator(calculator)