Exemple #1
0
    def from_file(cls, filepath, primitive=True, sort=False):
        """
        Reads a structure from a file. For example, anything ending in
        a "cif" is assumed to be a Crystallographic Information Format file.
        Supported formats include CIF, POSCAR/CONTCAR, CHGCAR, LOCPOT,
        vasprun.xml, CSSR, Netcdf and pymatgen's JSON serialized structures.

        Args:
            filename (str): The filename to read from.
            primitive (bool): Whether to convert to a primitive cell
                Only available for cifs, POSCAR, CSSR, JSON, YAML
                Defaults to True.
            sort (bool): Whether to sort sites. Default to False.

        Returns:
            :class:`Structure` object
        """
        if filepath.endswith(".nc"):
            from pymatgen.io.abinitio.netcdf import as_etsfreader
            file, closeit = as_etsfreader(filepath)

            new = file.read_structure(cls=cls)
            new.set_spacegroup(SpaceGroup.from_file(file))
            if closeit: file.close()

        else:
            # TODO: Spacegroup is missing here.
            new = super(Structure, cls).from_file(filepath, primitive=primitive, sort=sort)
            # Change the class of new.
            if new.__class__ != cls: new.__class__ = cls

        return new
Exemple #2
0
    def from_file(cls, filepath):
        """
        Return a new Structure instance from a NetCDF file 

        Args:
            filename:
                netcdf file with crystallographic data in the ETSF-IO format.
                or any other file format supported by `pymatgen.io.smartio`.
        """
        if filepath.endswith(".nc"):
            file, closeit = as_etsfreader(filepath)

            new = file.read_structure()
            # Change the class of new.
            new.__class__ = cls

            new.set_spacegroup(SpaceGroup.from_file(file))

            if closeit:
                file.close()
        else:
            # TODO: Spacegroup is missing here.
            #from pymatgen.io.smartio import read_structure
            #new = read_structure(filepath)
            new = super(Structure, cls).from_file(filepath)
            # Change the class of new.
            new.__class__ = cls

        return new
Exemple #3
0
    def from_file(cls, file, inord="F"):
        """Initialize the object from a Netcdf file."""
        file, closeit = as_etsfreader(file)

        new = cls(spgid=file.read_value("space_group"),
                  symrel=file.read_value("reduced_symmetry_matrices"),
                  tnons=file.read_value("reduced_symmetry_translations"),
                  symafm=file.read_value("symafm"),
                  has_timerev=True,  # FIXME not treated by ETSF-IO.
                  inord=inord,
                  )
        if closeit:
            file.close()

        return new
Exemple #4
0
    def from_file(cls, ncfile, inord="F"):
        """Initialize the object from a Netcdf file."""
        r, closeit = as_etsfreader(ncfile)

        kptopt = int(r.read_value("kptopt", default=1))

        new = cls(spgid=r.read_value("space_group"),
                  symrel=r.read_value("reduced_symmetry_matrices"),
                  tnons=r.read_value("reduced_symmetry_translations"),
                  symafm=r.read_value("symafm"),
                  has_timerev=has_timrev_from_kptopt(kptopt),
                  inord=inord)

        if closeit:
            file.close()

        return new
Exemple #5
0
    def from_file(cls, ncfile, inord="F"):
        """Initialize the object from a Netcdf file."""
        r, closeit = as_etsfreader(ncfile)

        kptopt = int(r.read_value("kptopt", default=1))

        new = cls(spgid=r.read_value("space_group"),
                  symrel=r.read_value("reduced_symmetry_matrices"),
                  tnons=r.read_value("reduced_symmetry_translations"),
                  symafm=r.read_value("symafm"),
                  has_timerev=has_timrev_from_kptopt(kptopt),
                  inord=inord)

        if closeit:
            file.close()

        return new
Exemple #6
0
    def from_file(cls, filepath):
        """
        Initialize the object from a Netcdf file with data
        saved in the ETSF-IO format.
        """
        file, closeit = as_etsfreader(filepath)

        d = {}
        for k in cls.KNOWN_KEYS:
            try:
                d[k] = file.read_value(k)
            except file.Error:
                pass

        if closeit:
            file.close()

        return cls(**d)
Exemple #7
0
    def from_file(cls, filepath, primitive=True, sort=False):
        """
        Reads a structure from a file. For example, anything ending in
        a "cif" is assumed to be a Crystallographic Information Format file.
        Supported formats include CIF, POSCAR/CONTCAR, CHGCAR, LOCPOT,
        vasprun.xml, CSSR, Netcdf and pymatgen's JSON serialized structures.

        Netcdf files supported:
            All files produced by ABINIT with info of the crystalline geometry 
            HIST_FILEs, in this case the last structure of the history is returned.

        Args:
            filename (str): The filename to read from.
            primitive (bool): Whether to convert to a primitive cell
                Only available for cifs, POSCAR, CSSR, JSON, YAML
                Defaults to True.
            sort (bool): Whether to sort sites. Default to False.

        Returns:
            :class:`Structure` object
        """
        if filepath.endswith(".nc"):
            from pymatgen.io.abinitio.netcdf import as_etsfreader
            file, closeit = as_etsfreader(filepath)

            new = file.read_structure(cls=cls)
            new.set_spacegroup(SpaceGroup.from_file(file))
            if closeit: file.close()

        elif filepath.endswith("_HIST") or filepath.endswith("_HIST.nc"):
            # Abinit history file. In this case we return the last structure!
            from abipy.dynamics.hist import HistFile
            with HistFile(filepath) as hist:
                return hist.structures[-1]

        else:
            # TODO: Spacegroup is missing here.
            new = super(Structure, cls).from_file(filepath,
                                                  primitive=primitive,
                                                  sort=sort)
            # Change the class of new.
            if new.__class__ != cls: new.__class__ = cls

        return new
Exemple #8
0
    def from_file(cls, filepath, primitive=True, sort=False):
        """
        Reads a structure from a file. For example, anything ending in
        a "cif" is assumed to be a Crystallographic Information Format file.
        Supported formats include CIF, POSCAR/CONTCAR, CHGCAR, LOCPOT,
        vasprun.xml, CSSR, Netcdf and pymatgen's JSON serialized structures.

        Netcdf files supported:
            All files produced by ABINIT with info of the crystalline geometry 
            HIST_FILEs, in this case the last structure of the history is returned.

        Args:
            filename (str): The filename to read from.
            primitive (bool): Whether to convert to a primitive cell
                Only available for cifs, POSCAR, CSSR, JSON, YAML
                Defaults to True.
            sort (bool): Whether to sort sites. Default to False.

        Returns:
            :class:`Structure` object
        """
        if filepath.endswith("_HIST") or filepath.endswith("_HIST.nc"):
            # Abinit history file. In this case we return the last structure!
            # Note that HIST does not follow the etsf-io conventions.
            from abipy.dynamics.hist import HistFile
            with HistFile(filepath) as hist:
                return hist.structures[-1]

        elif filepath.endswith(".nc"):
            from pymatgen.io.abinit.netcdf import as_etsfreader
            file, closeit = as_etsfreader(filepath)
                                                                  
            new = file.read_structure(cls=cls)
            new.set_spacegroup(SpaceGroup.from_file(file))
            if closeit: file.close()

        else:
            # TODO: Spacegroup is missing here.
            new = super(Structure, cls).from_file(filepath, primitive=primitive, sort=sort)
            # Change the class of new.
            if new.__class__ != cls: new.__class__ = cls

        return new
Exemple #9
0
    def from_file(cls, file, inord="F"):
        """Initialize the object from a Netcdf file."""
        #is_ncfile = is_string(file) and file.endswith(".nc")
        #if not is_ncfile
        #structure = Structure.from_file(file):
        #return cls.from_structure(cls, structure, has_timerev=True,  symprec=1e-5, angle_tolerance=5)

        file, closeit = as_etsfreader(file)

        new = cls(spgid=file.read_value("space_group"),
                  symrel=file.read_value("reduced_symmetry_matrices"),
                  tnons=file.read_value("reduced_symmetry_translations"),
                  symafm=file.read_value("symafm"),
                  has_timerev=True,  # FIXME not treated by ETSF-IO.
                  inord=inord)

        if closeit:
            file.close()

        return new
Exemple #10
0
    def from_file(cls, file, inord="F"):
        """Initialize the object from a Netcdf file."""
        #is_ncfile = is_string(file) and file.endswith(".nc")
        #if not is_ncfile
        #structure = Structure.from_file(file):
        #return cls.from_structure(cls, structure, has_timerev=True,  symprec=1e-5, angle_tolerance=5)

        file, closeit = as_etsfreader(file)

        new = cls(
            spgid=file.read_value("space_group"),
            symrel=file.read_value("reduced_symmetry_matrices"),
            tnons=file.read_value("reduced_symmetry_translations"),
            symafm=file.read_value("symafm"),
            has_timerev=True,  # FIXME not treated by ETSF-IO.
            inord=inord)

        if closeit:
            file.close()

        return new
Exemple #11
0
def kpoints_factory(filepath):
    """
    Factory function: returns an instance of [Kpath, IrredZone]
    from a netcdf file written according to the ETSF-IO specifications.
    """
    file, closeit = as_etsfreader(filepath)
    structure = file.read_structure()

    kinfo = KpointsInfo.from_file(filepath)

    if kinfo.is_sampling:
        obj = IrredZone(structure.reciprocal_lattice, kinfo.frac_coords, kinfo)

    elif kinfo.is_path:
        obj = Kpath(structure.reciprocal_lattice, kinfo.frac_coords, kinfo)

    else:
        raise ValueError("Only path or sampling modes are supported!")

    if closeit:
        file.close()

    return obj