def write(cls, fname=None, structure_format=None, **kwargs): """Write structure data to file. Parameters ---------- fname : str, optional structure_format : {None, str}, optional """ if fname is None and structure_format is None: structure_format = default_structure_format if fname is None: fname = get_fpath(fname='structure_data', ext=structure_format, add_fnum=True) if (structure_format is None and fname.endswith('.data')) or \ structure_format == 'data': from ._lammps_data_format import DATAWriter DATAWriter.write(fname=fname, **kwargs) elif (structure_format is None and fname.endswith('.dump')) or \ structure_format == 'dump': from ._lammps_dump_format import DUMPWriter DUMPWriter.write(fname=fname, **kwargs) # elif (structure_format is None and fname.endswith('.xyz')) or \ # structure_format == 'xyz': else: from ._xyz_format import XYZWriter XYZWriter.write(fname=fname, **kwargs)
def write(cls, fname=None, outpath=None, fpath=None, atoms=None, comment_line=None): """Write structure data to file. Parameters ---------- fname : str, optional Output file name. outpath : str, optional Output file path. fpath : str, optional Full path (directory path + file name) to output data file. atoms : :py:class:`~sknano.io.atoms.Atoms` An :py:class:`~sknano.io.atoms.Atoms` instance. comment_line : str, optional """ if fpath is None: fpath = get_fpath(fname=fname, ext='pdb', outpath=outpath, overwrite=True, add_fnum=False) if comment_line is None: comment_line = default_comment_line atoms.rezero_coords() with open(fpath, 'w') as f: for atom in atoms: f.write(PDBFormatter.format(atom))
def write(cls, fname=None, outpath=None, fpath=None, atoms=None, comment_line=None): """Write structure data to file. Parameters ---------- fname : str, optional Output file name. outpath : str, optional Output file path. fpath : str, optional Full path (directory path + file name) to output data file. atoms : :class:`~sknano.io.atoms.Atoms` An :class:`~sknano.io.atoms.Atoms` instance. comment_line : str, optional A string written to the first line of `zmatrix` file. If `None`, then it is set to the full path of the output `zmatrix` file. """ if fpath is None: fpath = get_fpath(fname=fname, ext='zmatrix', outpath=outpath, overwrite=True, add_fnum=False) print('fpath: {}'.format(fpath)) if comment_line is None: comment_line = default_comment_line atoms.rezero_coords()
def update_app_view(self): self.nanogen_status_bar.showMessage('Ready.') if self.generator_controller is not None: kwargs = self.generator_controller.get_generator_parameters() generator = getattr(importlib.import_module('sknano.generators'), kwargs['generator_class']) structure_format = \ str(self.structure_format_combo_box.itemText( self.structure_format_combo_box.currentIndex())) fpath = get_fpath(fname=generator.generate_fname(**kwargs), ext=structure_format, outpath=os.getcwd(), overwrite=False, add_fnum=True) self.fpath_line_edit.setText(fpath)
def write(cls, fname=None, outpath=None, fpath=None, structure=None, atoms=None, comment_line=None, **kwargs): """Write structure data to file. Parameters ---------- fname : str, optional Output file name. outpath : str, optional Output file path. fpath : str, optional Full path (directory path + file name) to output data file. atoms : :class:`~sknano.core.atoms.Atoms` An :class:`~sknano.core.atoms.Atoms` instance. comment_line : str, optional A string written to the first line of `xyz` file. If `None`, then it is set to the full path of the output `xyz` file. """ if structure is None and atoms is None: raise ValueError('Expected either `structure` or `atoms` object.') if structure is not None and atoms is None: atoms = structure.atoms if fpath is None: fpath = get_fpath(fname=fname, ext='xyz', outpath=outpath, overwrite=True, add_fnum=False) if comment_line is None: comment_line = default_comment_line atoms.rezero_coords() with zopen(fpath, 'wt') as f: f.write('{:d}\n'.format(atoms.Natoms)) f.write('{}\n'.format(comment_line)) for atom in atoms: f.write('{:>3s}{:15.8f}{:15.8f}{:15.8f}\n'.format( atom.symbol, atom.x, atom.y, atom.z))
def write(cls, fname=None, outpath=None, fpath=None, atoms=None, comment_line=None, verbose=False, **kwargs): """Write structure dump to file. Parameters ---------- fname : str, optional Output file name. outpath : str, optional Output file path. fpath : str, optional Full path (directory path + file name) to output data file. atoms : `Atoms` An :py:class:`Atoms` instance. boxbounds : dict, optional If `None`, determined automatically from atom coordinates. comment_line : str, optional A string written to the first line of `dump` file. If `None`, then it is set to the full path of the output `dump` file. assert_unique_ids : bool, optional Check that each Atom in Atoms has a unique ID. If the check fails, then assign a unique ID to each Atom. If `assert_unique_ids` is True, but the ID's are not unique, LAMMPS will not be able to read the dump file. verbose : bool, optional verbose output """ if fpath is None: fpath = get_fpath(fname=fname, ext='dump', outpath=outpath, overwrite=True, add_fnum=False) if comment_line is None: comment_line = default_comment_line atoms.rezero_coords()
def write(cls, fname=None, outpath=None, fpath=None, structure=None, atoms=None, atom_style='full', bounding_box=None, comment_line=None, assert_unique_ids=False, enforce_consecutive_ids=True, pad_box=False, xpad=10., ypad=10., zpad=10., pad_tol=0.01, verbose=False, **kwargs): """Write structure data to file. Parameters ---------- fname : str, optional Output file name. outpath : str, optional Output file path. fpath : str, optional Full path (directory path + file name) to output data file. atoms : :class:`~sknano.core.atoms.Atoms` An :class:`~sknano.core.atoms.Atoms` instance. bounding_box : dict, optional If `None`, determined automatically from the `atoms` coordinates. comment_line : str, optional A string written to the first line of `data` file. If `None`, then it is set to the full path of the output `data` file. assert_unique_ids : bool, optional Check that each :class:`~sknano.core.atoms.Atom` in `atoms` has a unique :attr:`~sknano.core.atoms.Atom.id`. If the check fails, then assign a unique :attr:`~sknano.core.atoms.Atom.id`. to each :class:`~sknano.core.atoms.Atom`. If `assert_unique_ids` is True, but the id's are not unique, LAMMPS will not be able to read the data file. enforce_consecutive_ids : bool, optional pad_box : bool, optional xpad, ypad, zpad : float, optional pad_tol : float, optional verbose : bool, optional verbose output """ if structure is None and atoms is None: raise ValueError('Expected either `structure` or `atoms` object.') if structure is not None and atoms is None: atoms = structure.atoms if fpath is None: fpath = get_fpath(fname=fname, ext='data', outpath=outpath, overwrite=True, add_fnum=False) if comment_line is None: comment_line = default_comment_line atoms.rezero() atoms.assign_unique_types() typemap = atoms.typemap Natoms = atoms.Natoms Natoms_width = \ 8 if len(str(Natoms)) <= 12 else len(str(Natoms)) + 4 Ntypes = atoms.Ntypes Ntypes_width = Natoms_width id_width = len(str(Natoms)) + 1 type_width = len(str(Ntypes)) + 1 if (enforce_consecutive_ids and atoms.ids.max() != atoms.Natoms) or \ (not assert_unique_ids and len(set(atoms.ids)) != atoms.Natoms): atoms.assign_unique_ids() if bounding_box is None: if structure is not None and structure.lattice is not None: bounding_box = \ generate_bounding_box(from_lattice=structure.lattice, center=atoms.centroid) else: bounding_box = \ generate_bounding_box(from_array=atoms.coords) if pad_box: boxpad = {'x': xpad, 'y': ypad, 'z': zpad} # for dim, pad in boxpad.items(): for i, dim in enumerate(('x', 'y', 'z')): pad = boxpad[dim] dmin = dim + 'min' dmax = dim + 'max' if abs(getattr(bounding_box, dmin) - atoms.coords[:, i].min()) < pad - pad_tol: setattr(bounding_box, dmin, getattr(bounding_box, dmin) - pad) if abs(getattr(bounding_box, dmax) - atoms.coords[:, i].max()) < pad - pad_tol: setattr(bounding_box, dmax, getattr(bounding_box, dmax) + pad) if verbose: print('bounding_box: {}'.format(bounding_box)) lohi_width = 0 for dim in ('x', 'y', 'z'): lohi_width = \ max(lohi_width, len('{:.6f} {:.6f}'.format( getattr(bounding_box, dim + 'min'), getattr(bounding_box, dim + 'max'))) + 4) with zopen(fpath, 'wt') as f: f.write('# {}\n\n'.format(comment_line.lstrip('#').strip())) f.write('{}atoms\n'.format( '{:d}'.format(Natoms).ljust(Natoms_width))) f.write('{}atom types\n\n'.format( '{:d}'.format(Ntypes).ljust(Ntypes_width))) for dim in ('x', 'y', 'z'): f.write('{}{dim}lo {dim}hi\n'.format( '{:.6f} {:.6f}'.format( getattr(bounding_box, dim + 'min'), getattr(bounding_box, dim + 'max')).ljust(lohi_width), dim=dim)) f.write('\nMasses\n\n') for atomtype, properties in list(typemap.items()): f.write('{}{:.4f}\n'.format( '{:d}'.format(atomtype).ljust(Natoms_width), properties['mass'])) f.write('\nAtoms\n\n') for atom in atoms: line = '' line += "{:>{}}".format(atom.id, id_width) line += "{:>{}}".format(atom.mol, 3) line += "{:>{}}".format(atom.type, type_width) line += "{:>{}}".format('{:.1f}'.format(atom.q), 4) line += "{:>{}}".format('{:f}'.format(atom.x), 14) line += "{:>{}}".format('{:f}'.format(atom.y), 14) line += "{:>{}}".format('{:f}'.format(atom.z), 14) line += "{:>{}}".format('{:d}'.format(atom.ix), 3) line += "{:>{}}".format('{:d}'.format(atom.iy), 3) line += "{:>{}}".format('{:d}'.format(atom.iz), 3) line += '\n' f.write(line) f.write('\nVelocities\n\n') for atom in atoms: line = '' line += "{:>{}}".format(atom.id, id_width) line += "{:>{}}".format('{:f}'.format(atom.vx), 14) line += "{:>{}}".format('{:f}'.format(atom.vy), 14) line += "{:>{}}".format('{:f}'.format(atom.vz), 14) line += '\n' f.write(line)
def write(cls, fname=None, outpath=None, fpath=None, structure=None, atoms=None, atom_style='full', bounding_box=None, comment_line=None, assert_unique_ids=False, enforce_consecutive_ids=True, pad_box=False, xpad=10., ypad=10., zpad=10., pad_tol=0.01, verbose=False, **kwargs): """Write structure data to file. Parameters ---------- fname : str, optional Output file name. outpath : str, optional Output file path. fpath : str, optional Full path (directory path + file name) to output data file. atoms : :class:`~sknano.core.atoms.Atoms` An :class:`~sknano.core.atoms.Atoms` instance. bounding_box : dict, optional If `None`, determined automatically from the `atoms` coordinates. comment_line : str, optional A string written to the first line of `data` file. If `None`, then it is set to the full path of the output `data` file. assert_unique_ids : bool, optional Check that each :class:`~sknano.core.atoms.Atom` in `atoms` has a unique :attr:`~sknano.core.atoms.Atom.id`. If the check fails, then assign a unique :attr:`~sknano.core.atoms.Atom.id`. to each :class:`~sknano.core.atoms.Atom`. If `assert_unique_ids` is True, but the id's are not unique, LAMMPS will not be able to read the data file. enforce_consecutive_ids : bool, optional pad_box : bool, optional xpad, ypad, zpad : float, optional pad_tol : float, optional verbose : bool, optional verbose output """ if structure is None and atoms is None: raise ValueError('Expected either `structure` or `atoms` object.') if structure is not None and atoms is None: atoms = structure.atoms if fpath is None: fpath = get_fpath(fname=fname, ext='data', outpath=outpath, overwrite=True, add_fnum=False) if comment_line is None: comment_line = default_comment_line atoms.rezero() atoms.assign_unique_types() typemap = atoms.typemap Natoms = atoms.Natoms Natoms_width = \ 8 if len(str(Natoms)) <= 12 else len(str(Natoms)) + 4 Ntypes = atoms.Ntypes Ntypes_width = Natoms_width id_width = len(str(Natoms)) + 1 type_width = len(str(Ntypes)) + 1 if (enforce_consecutive_ids and atoms.ids.max() != atoms.Natoms) or \ (not assert_unique_ids and len(set(atoms.ids)) != atoms.Natoms): atoms.assign_unique_ids() if bounding_box is None: if structure is not None and structure.lattice is not None: bounding_box = \ generate_bounding_box(from_lattice=structure.lattice, center=atoms.centroid) else: bounding_box = \ generate_bounding_box(from_array=atoms.coords) if pad_box: boxpad = {'x': xpad, 'y': ypad, 'z': zpad} # for dim, pad in boxpad.items(): for i, dim in enumerate(('x', 'y', 'z')): pad = boxpad[dim] dmin = dim + 'min' dmax = dim + 'max' if abs(getattr(bounding_box, dmin) - atoms.coords[:, i].min()) < pad - pad_tol: setattr(bounding_box, dmin, getattr(bounding_box, dmin) - pad) if abs(getattr(bounding_box, dmax) - atoms.coords[:, i].max()) < pad - pad_tol: setattr(bounding_box, dmax, getattr(bounding_box, dmax) + pad) if verbose: print('bounding_box: {}'.format(bounding_box)) lohi_width = 0 for dim in ('x', 'y', 'z'): lohi_width = \ max(lohi_width, len('{:.6f} {:.6f}'.format( getattr(bounding_box, dim + 'min'), getattr(bounding_box, dim + 'max'))) + 4) with zopen(fpath, 'wt') as f: f.write('# {}\n\n'.format(comment_line.lstrip('#').strip())) f.write('{}atoms\n'.format( '{:d}'.format(Natoms).ljust(Natoms_width))) f.write('{}atom types\n\n'.format( '{:d}'.format(Ntypes).ljust(Ntypes_width))) for dim in ('x', 'y', 'z'): f.write('{}{dim}lo {dim}hi\n'.format('{:.6f} {:.6f}'.format( getattr(bounding_box, dim + 'min'), getattr(bounding_box, dim + 'max')).ljust(lohi_width), dim=dim)) f.write('\nMasses\n\n') for atomtype, properties in list(typemap.items()): f.write('{}{:.4f}\n'.format( '{:d}'.format(atomtype).ljust(Natoms_width), properties['mass'])) f.write('\nAtoms\n\n') for atom in atoms: line = '' line += "{:>{}}".format(atom.id, id_width) line += "{:>{}}".format(atom.mol, 3) line += "{:>{}}".format(atom.type, type_width) line += "{:>{}}".format('{:.1f}'.format(atom.q), 4) line += "{:>{}}".format('{:f}'.format(atom.x), 14) line += "{:>{}}".format('{:f}'.format(atom.y), 14) line += "{:>{}}".format('{:f}'.format(atom.z), 14) line += "{:>{}}".format('{:d}'.format(atom.ix), 3) line += "{:>{}}".format('{:d}'.format(atom.iy), 3) line += "{:>{}}".format('{:d}'.format(atom.iz), 3) line += '\n' f.write(line) f.write('\nVelocities\n\n') for atom in atoms: line = '' line += "{:>{}}".format(atom.id, id_width) line += "{:>{}}".format('{:f}'.format(atom.vx), 14) line += "{:>{}}".format('{:f}'.format(atom.vy), 14) line += "{:>{}}".format('{:f}'.format(atom.vz), 14) line += '\n' f.write(line)