def test_to_from_dict(self): d = self.tetragonal.to_dict t = Lattice.from_dict(d) for i in range(3): self.assertEqual(t.abc[i], self.tetragonal.abc[i]) self.assertEqual(t.angles[i], self.tetragonal.angles[i]) #Make sure old style dicts work. del d["matrix"] t = Lattice.from_dict(d) for i in range(3): self.assertEqual(t.abc[i], self.tetragonal.abc[i]) self.assertEqual(t.angles[i], self.tetragonal.angles[i])
def change_lattice_constants(structure, lattice, scale=False): ''' Change lattice constant in a pymatgen strucure Args: structure: pymatgen strucure lattice: a 3x3 list, like [[None,1.0,None], [None,None,None], [None,None,15]] where None means this item will not be changed scale: is true when the lattice need to scale, i.e. latt * scale is False when the new lattice directly past to the structure Return: changed pymatgen structure ''' latt = structure.lattice.as_dict() for i in range(3): for j in range(3): if lattice[i][j] is not None: if scale: latt['matrix'][i][j] = lattice[i][j] * latt['matrix'][i][j] else: latt['matrix'][i][j] = lattice[i][j] latt = Lattice.from_dict(latt) structure.lattice = latt return structure
def from_dict(cls, d, lattice=None): """ Create PeriodicSite from dict representation. Args: d (dict): dict representation of PeriodicSite lattice: Optional lattice to override lattice specified in d. Useful for ensuring all sites in a structure share the same lattice. Returns: PeriodicSite """ atoms_n_occu = {} for sp_occu in d["species"]: if "oxidation_state" in sp_occu and Element.is_valid_symbol( sp_occu["element"]): sp = Specie.from_dict(sp_occu) elif "oxidation_state" in sp_occu: sp = DummySpecie.from_dict(sp_occu) else: sp = Element(sp_occu["element"]) atoms_n_occu[sp] = sp_occu["occu"] props = d.get("properties", None) lattice = lattice if lattice else Lattice.from_dict(d["lattice"]) return cls(atoms_n_occu, d["abc"], lattice, properties=props)
def from_dict(cls, d): """ Makes Kpoints obey the general json interface used in pymatgen for easier serialization. """ reciprocal_lattice = Lattice.from_dict(d["reciprocal_lattice"]) return cls(reciprocal_lattice, d["frac_coords"], weights=d["weights"], names=d["names"], ksampling=d["ksampling"])
def from_dict(cls, d, lattice=None): """ Create PeriodicSite from dict representation. Args: d (dict): dict representation of PeriodicSite lattice: Optional lattice to override lattice specified in d. Useful for ensuring all sites in a structure share the same lattice. Returns: PeriodicSite """ species = {} for sp_occu in d["species"]: if "oxidation_state" in sp_occu and Element.is_valid_symbol( sp_occu["element"]): sp = Species.from_dict(sp_occu) elif "oxidation_state" in sp_occu: sp = DummySpecies.from_dict(sp_occu) else: sp = Element(sp_occu["element"]) species[sp] = sp_occu["occu"] props = d.get("properties", None) if props is not None: for key in props.keys(): props[key] = json.loads(json.dumps(props[key], cls=MontyEncoder), cls=MontyDecoder) lattice = lattice if lattice else Lattice.from_dict(d["lattice"]) return cls(species, d["abc"], lattice, properties=props)
def from_dict(d): """ Args: a dictionnary with all data for a band structure symm line object Returns: a BandStructureSymmLine object """ labels_dict = d['labels_dict'] return BandStructureSymmLine(d['kpoints'], d['bands'], Lattice.from_dict(d['lattice_rec']), d['efermi'], labels_dict)
def from_dict(cls, d): lattice = Lattice.from_dict(d["lattice"]) sites = [PeriodicSite.from_dict(sd, lattice) for sd in d["sites"]] s = Structure.from_sites(sites) return Slab( lattice=lattice, species=s.species_and_occu, coords=s.frac_coords, miller_index=d["miller_index"], oriented_unit_cell=Structure.from_dict(d["oriented_unit_cell"]), shift=d["shift"], scale_factor=d["scale_factor"], site_properties=s.site_properties, energy=d["energy"] )
def from_dict(d): """ Reconstitute a Structure object from a dict representation of Structure created using to_dict. Args: d: dict representation of structure. Returns: Structure object """ lattice = Lattice.from_dict(d["lattice"]) sites = [PeriodicSite.from_dict(sd, lattice) for sd in d["sites"]] return Structure.from_sites(sites)
def from_dict(cls, d): """ Create from dict. Args: A dict with all data for a kpoint object. Returns: A Kpoint object """ return cls( coords=d["fcoords"], lattice=Lattice.from_dict(d["lattice"]), coords_are_cartesian=False, label=d["label"], )
def from_dict(d, lattice=None): """ Create PeriodicSite from dict representation. Args: d: dict representation of PeriodicSite lattice: Optional lattice to override lattice specified in d. Useful for ensuring all sites in a structure share the same lattice. """ atoms_n_occu = {} for sp_occu in d["species"]: sp = Specie.from_dict(sp_occu) if "oxidation_state" in sp_occu \ else Element(sp_occu["element"]) atoms_n_occu[sp] = sp_occu["occu"] props = d.get("properties", None) lattice = lattice if lattice else Lattice.from_dict(d["lattice"]) return PeriodicSite(atoms_n_occu, d["abc"], lattice, properties=props)
def from_dict(cls, d): """ :param d: dict :return: Creates slab from dict. """ lattice = Lattice.from_dict(d["lattice"]) sites = [PeriodicSite.from_dict(sd, lattice) for sd in d["sites"]] s = Structure.from_sites(sites) optional = dict( in_plane_offset=d.get("in_plane_offset"), gap=d.get("gap"), vacuum_over_film=d.get("vacuum_over_film"), interface_properties=d.get("interface_properties"), ) return Interface( lattice=lattice, species=s.species_and_occu, coords=s.frac_coords, site_properties=s.site_properties, **{k: v for k, v in optional.items() if v is not None}, )
def from_dict(cls, d): """ :param d: Dict representation :return: Interface """ lattice = Lattice.from_dict(d["lattice"]) sites = [PeriodicSite.from_dict(sd, lattice) for sd in d["sites"]] s = Structure.from_sites(sites) return Interface( lattice=lattice, species=s.species_and_occu, coords=s.frac_coords, sub_plane=d["sub_plane"], film_plane=d["film_plane"], sub_init_cell=d["sub_init_cell"], film_init_cell=d["film_init_cell"], modified_sub_structure=d["modified_sub_structure"], modified_film_structure=d["modified_film_structure"], strained_sub_structure=d["strained_sub_structure"], strained_film_structure=d["strained_film_structure"], site_properties=s.site_properties, init_inplane_shift=d["init_inplane_shift"], )
def generate(eps, slab_d): """ Generate SPHInX input file. eps (float): averaged dielectric constant slab_d (float): corresponding slab thickness (Angstroms) """ ## the main script should have put us in the appropriate subdirectory dir_sub = os.getcwd() with open(os.path.join(dir_sub, "defectproperty.json"), 'r') as file: defprop = json.loads(file.read()) lattice = Lattice.from_dict(defprop["lattice"]) ## STRUCTURE GROUP s = structure_grp(lattice) ## SLAB GROUP ## assume slab is centered in the cell vertically slabmin = (lattice.c - slab_d) / 2 slabmax = (lattice.c + slab_d) / 2 s += slab_grp(slabmin, slabmax, eps=eps * np.eye(3)) ## CHARGE GROUP posZ = np.mean([def_site[2] for def_site in defprop["defect_site"]]) s += charge_grp(posZ * lattice.c, defprop["charge"]) ## ISOLATED GROUP s += isolated_grp(slabmin, slabmax) if not os.path.exists(os.path.join(dir_sub, "correction")): os.makedirs(os.path.join(dir_sub, "correction")) with open(os.path.join(dir_sub, "correction", "system.sx"), 'w') as f: f.write(s)
def structure_from_abivars(cls=None, *args, **kwargs): """ Build a :class:`Structure` object from a dictionary with ABINIT variables. Args: cls: Structure class to be instantiated. pymatgen.core.structure.Structure if cls is None example: al_structure = structure_from_abivars( acell=3*[7.5], rprim=[0.0, 0.5, 0.5, 0.5, 0.0, 0.5, 0.5, 0.5, 0.0], typat=1, xred=[0.0, 0.0, 0.0], ntypat=1, znucl=13, ) `xred` can be replaced with `xcart` or `xangst`. """ kwargs.update(dict(*args)) d = kwargs cls = Structure if cls is None else cls lattice = Lattice.from_dict(d, fmt="abivars") coords, coords_are_cartesian = d.get("xred", None), False if coords is None: coords = d.get("xcart", None) if coords is not None: if "xangst" in d: raise ValueError("xangst and xcart are mutually exclusive") coords = ArrayWithUnit(coords, "bohr").to("ang") else: coords = d.get("xangst", None) coords_are_cartesian = True if coords is None: raise ValueError("Cannot extract coordinates from:\n %s" % str(d)) coords = np.reshape(coords, (-1,3)) znucl_type, typat = d["znucl"], d["typat"] if not isinstance(znucl_type, collections.Iterable): znucl_type = [znucl_type] if not isinstance(typat, collections.Iterable): typat = [typat] assert len(typat) == len(coords) # Note Fortran --> C indexing #znucl_type = np.rint(znucl_type) species = [znucl_type[typ-1] for typ in typat] return cls(lattice, species, coords, validate_proximity=False, to_unit_cell=False, coords_are_cartesian=coords_are_cartesian)
parser = argparse.ArgumentParser(description='Generate INCAR') parser.add_argument('eps', type=float, help='averaged dielectric constant') parser.add_argument('slab_d', type=float, help='corresponding slab thickness (Angstroms)') ## read in the above arguments from command line args = parser.parse_args() ## the bash script already put us in the appropriate subdirectory dir_sub = os.getcwd() # lattice = Poscar.from_file(folder1+"POSCAR").structure.lattice with open(os.path.join(dir_sub, "defectproperty.json"), 'r') as file: defprop = json.loads(file.read()) lattice = Lattice.from_dict(defprop["lattice"]) ## STRUCTURE GROUP s = structure_grp(lattice) ## SLAB GROUP ## assume slab is centered in the cell vertically slabmin = (lattice.c - args.slab_d) / 2 slabmax = (lattice.c + args.slab_d) / 2 s += slab_grp(slabmin, slabmax, eps=args.eps * np.eye(3)) ## CHARGE GROUP posZ = np.mean([def_site[2] for def_site in defprop["defect_site"]]) s += charge_grp(posZ * lattice.c, defprop["charge"]) ## ISOLATED GROUP