def export_ur2(self, filename, structure): """ Export the wavefunction on file filename. Format is defined by the extension in filename. """ if "." not in filename: raise ValueError("Cannot detect file extension in: %s" % filename) tokens = filename.strip().split(".") ext = tokens[-1] if not tokens[0]: # fname == ".ext" ==> Create temporary file. filename = tempfile.mkstemp(suffix="." + ext, text=True)[1] print("Creating temporary file: %s" % filename) # Compute |u(r)|2 and write data according to ext. ur2 = np.reshape(self.ur2, (1,) + self.ur2.shape) with open(filename, mode="w") as fh: if ext == "xsf": # xcrysden xsf_write_structure(fh, structures=[structure]) xsf_write_data(fh, structure, ur2, add_replicas=True) else: raise NotImplementedError("extension %s is not supported." % ext) return Visualizer.from_file(filename)
def export(self, filename): """ Export the real space data on file filename. Format is defined by the extension in filename. See :class:`Visualizer` for the list of applications and formats supported. """ if "." not in filename: raise ValueError(" Cannot detect file extension in filename: %s " % filename) tokens = filename.strip().split(".") ext = tokens[-1] if not tokens[0]: # filename == ".ext" ==> Create temporary file. import tempfile filename = tempfile.mkstemp(suffix="."+ext, text=True)[1] with open(filename, mode="w") as fh: if ext == "xsf": # xcrysden xsf.xsf_write_structure(fh, self.structure) xsf.xsf_write_data(fh, self.structure, self.datar, add_replicas=True) else: raise NotImplementedError("extension %s is not supported." % ext) return Visualizer.from_file(filename)
def export(self, filename): """ Export the crystalline structure on file filename. Returns: Instance of :class:`Visualizer` The format is defined by the extension in filename: See :class:`Visualizer` for the list of applications and formats supported. #. "prefix.xsf" for XcrysDen files. An *empty* prefix, e.g. ".xsf" makes the code use a temporary file. """ if "." not in filename: raise ValueError("Cannot detect extension in filename %s: " % filename) tokens = filename.strip().split(".") ext = tokens[-1] if not tokens[0]: # filename == ".ext" ==> Create temporary file. import tempfile filename = tempfile.mkstemp(suffix="."+ext, text=True)[1] with open(filename, mode="w") as fh: if ext == "xsf": # xcrysden xsf.xsf_write_structure(fh, structures=[self]) else: raise Visualizer.Error("extension %s is not supported." % ext) return Visualizer.from_file(filename)
def visualize_qpoint_nu(self, qpoint, nu, spin=0, appname="vesta"): iq, qpoint = self._find_iqpt_qpoint(qpoint) # Fortran array nctkarr_t("v1_qnu", "dp", "two, nfft, nspden, natom3, nqlist")]) v1_qnu = self.reader.read_variable("v1_qnu")[iq, nu, spin] v1_qnu = v1_qnu[:, 0] + 1j * v1_qnu[:, 1] #wqnu = self.reader.read_variable["phfreqs"][nu] #v1_qnu /= np.sqrt(2 * wqnu) datar = np.reshape(np.abs(v1_qnu), self.ngfft) visu = Visualizer.from_name(appname) ext = "xsf" if ext not in visu.supported_extensions(): raise ValueError("Visualizer %s does not support XSF files" % visu) from abipy.core.globals import abinb_mkstemp _, filename = abinb_mkstemp(suffix="." + ext, text=True) with open(filename, mode="wt") as fh: if ext == "xsf": xsf.xsf_write_structure(fh, self.structure) xsf.xsf_write_data(fh, self.structure, datar, add_replicas=True) else: raise NotImplementedError("extension %s is not supported." % ext) return visu(filename)
def vesta_open(self, temp=300): # pragma: no cover """ Visualize termal displacement ellipsoids at temperature `temp` (Kelvin) with Vesta_ application. """ filepath = self.write_cif_file(filepath=None, temp=temp) cprint("Writing structure + Debye-Waller tensor in CIF format for T = %s (K) to file: %s" % (temp, filepath), "green") cprint("In the Vesta GUI, select: Properties -> Atoms -> Show as displament ellipsoids.", "green") from abipy.iotools import Visualizer visu = Visualizer.from_name("vesta") return visu(filepath)()
def export(self, filename, visu=None): """ Export the crystalline structure on file filename. Args: filename: String specifying the file path and the file format. The format is defined by the file extension. filename="prefix.xsf", for example, will produce a file in XSF format. An *empty* prefix, e.g. ".xsf" makes the code use a temporary file. visu: `Visualizer` subclass. By default, this method returns the first available visualizer that supports the given file format. If visu is not None, an instance of visu is returned. See :class:`Visualizer` for the list of applications and formats supported. Returns: Instance of :class:`Visualizer` """ if "." not in filename: raise ValueError("Cannot detect extension in filename %s: " % filename) tokens = filename.strip().split(".") ext = tokens[-1] if not tokens[0]: # filename == ".ext" ==> Create temporary file. import tempfile filename = tempfile.mkstemp(suffix="." + ext, text=True)[1] with open(filename, mode="w") as fh: if ext == "xsf": # xcrysden xsf.xsf_write_structure(fh, structures=[self]) else: raise Visualizer.Error("extension %s is not supported." % ext) if visu is None: return Visualizer.from_file(filename) else: return visu(filename)
def visualize_ur2(self, spin, kpoint, band, visu_name): """ Visualize :math:`|u(r)|^2` with visualizer. See :class:`Visualizer` for the list of applications and formats supported. """ visu = Visualizer.from_name(visu_name) for ext in visu.supported_extensions(): ext = "." + ext try: return self.export_ur2(ext, spin, kpoint, band, visu=visu) except visu.Error: pass else: raise visu.Error("Don't know how to export data for visualizer %s" % visu_name)
def visualize(self, appname="ovito"): # pragma: no cover """ Visualize the crystalline structure with visualizer. See :class:`Visualizer` for the list of applications and formats supported. """ if appname == "mayavi": return self.mayaview() # Get the Visualizer subclass from the string. from abipy.iotools import Visualizer visu = Visualizer.from_name(appname) if visu.name != "ovito": raise NotImplementedError("visualizer: %s" % visu.name) filepath = self.write_xdatcar(filepath=None, groupby_type=True) return visu(filepath)()
def visualize_ur2(self, spin, kpoint, band, visu_name): """ Visualize :math:`|u(r)|^2` with visualizer. See :class:`Visualizer` for the list of applications and formats supported. """ visu = Visualizer.from_name(visu_name) for ext in visu.supported_extensions(): ext = "." + ext try: return self.export_ur2(ext, spin, kpoint, band, visu=visu) except visu.Error: pass else: raise visu.Error( "Don't know how to export data for visualizer %s" % visu_name)
def visualize(self, visualizer): """ Visualize data with visualizer. See :class:`Visualizer` for the list of applications and formats supported. """ extensions = Visualizer.exts_from_appname(visualizer) for ext in extensions: ext = "." + ext try: return self.export(ext) except Visualizer.Error: pass else: raise Visualizer.Error("Don't know how to export data for visualizer %s" % visualizer)
def export(self, filename, visu=None, verbose=1): """ Export the real space data to file filename. Args: filename: String specifying the file path and the file format. The format is defined by the file extension. filename="prefix.xsf", for example, will produce a file in XSF format (xcrysden_). An *empty* prefix, e.g. ".xsf" makes the code use a temporary file. visu: :class:`Visualizer` subclass. By default, this method returns the first available visualizer that supports the given file format. If visu is not None, an instance of visu is returned. See :class:`Visualizer` for the list of applications and formats supported. verbose: Verbosity level Returns: Instance of :class:`Visualizer` """ if "." not in filename: raise ValueError("Cannot detect file extension in filename: %s " % filename) tokens = filename.strip().split(".") ext = tokens[-1] if verbose: print("tokens", tokens, "ext", ext) if not tokens[0]: # filename == ".ext" ==> Create temporary file. # dir = os.getcwd() is needed when we invoke the method from a notebook. # nbworkdir in cwd is needed when we invoke the method from a notebook. from abipy.core.globals import abinb_mkstemp _, filename = abinb_mkstemp(suffix="." + ext, text=True) with open(filename, mode="wt") as fh: if ext == "xsf": # xcrysden xsf.xsf_write_structure(fh, self.structure) xsf.xsf_write_data(fh, self.structure, self.datar, add_replicas=True) #elif ext == "POSCAR": else: raise NotImplementedError("extension %s is not supported." % ext) if visu is None: return Visualizer.from_file(filename) else: return visu(filename)
def export_ur2(self, filename, structure, visu=None): """ Export u(r)**2 on file filename. Args: filename: String specifying the file path and the file format. The format is defined by the file extension. filename="prefix.xsf", for example, will produce a file in XSF format. An *empty* prefix, e.g. ".xsf" makes the code use a temporary file. structure: Structure object. visu: `Visualizer` subclass. By default, this method returns the first available visualizer that supports the given file format. If visu is not None, an instance of visu is returned. See :class:`Visualizer` for the list of applications and formats supported. Returns: Instance of :class:`Visualizer` """ if "." not in filename: raise ValueError("Cannot detect file extension in: %s" % filename) tokens = filename.strip().split(".") ext = tokens[-1] if not tokens[0]: # fname == ".ext" ==> Create temporary file. filename = tempfile.mkstemp(suffix="." + ext, text=True)[1] print("Creating temporary file: %s" % filename) # Compute |u(r)|2 and write data according to ext. ur2 = np.reshape(self.ur2, (1,) + self.ur2.shape) with open(filename, mode="w") as fh: if ext == "xsf": # xcrysden xsf_write_structure(fh, structures=[structure]) xsf_write_data(fh, structure, ur2, add_replicas=True) else: raise NotImplementedError("extension %s is not supported." % ext) if visu is None: return Visualizer.from_file(filename) else: return visu(filename)
def export_ur2(self, filename, visu=None): """ Export :math:`|u(r)|^2` to file ``filename``. Args: filename: String specifying the file path and the file format. The format is defined by the file extension. filename="prefix.xsf", for example, will produce a file in XSF format. An *empty* prefix, e.g. ".xsf" makes the code use a temporary file. visu: :class:`Visualizer` subclass. By default, this method returns the first available visualizer that supports the given file format. If visu is not None, an instance of visu is returned. See :class:`Visualizer` for the list of applications and formats supported. Returns: Instance of :class:`Visualizer` """ if "." not in filename: raise ValueError("Cannot detect file extension in: %s" % filename) tokens = filename.strip().split(".") ext = tokens[-1] if not tokens[0]: # fname == ".ext" ==> Create temporary file. # dir = os.getcwd() is needed when we invoke the method from a notebook. from abipy.core.globals import abinb_mkstemp _, filename = abinb_mkstemp(suffix="." + ext, text=True) print("Creating temporary file: %s" % filename) # Compute |u(r)|2 and write data according to ext. ur2 = np.reshape(self.ur2, (1, ) + self.ur2.shape) with open(filename, mode="wt") as fh: if ext == "xsf": # xcrysden xsf_write_structure(fh, structures=self.structure) xsf_write_data(fh, self.structure, ur2, add_replicas=True) else: raise NotImplementedError("extension %s is not supported." % ext) if visu is None: return Visualizer.from_file(filename) else: return visu(filename)
def visualize(self, appname): """ Visualize data with visualizer. See :class:`Visualizer` for the list of applications and formats supported. """ visu = Visualizer.from_name(appname) # Try to export data to one of the formats supported by the visualizer # Use a temporary file (note "." + ext) for ext in visu.supported_extensions(): ext = "." + ext try: return self.export(ext, visu=visu)() except visu.Error: pass else: raise visu.Error("Don't know how to export data for visualizer %s" % appname)
def visualize(self, visu_name): """ Visualize the crystalline structure with visualizer. See :class:`Visualizer` for the list of applications and formats supported. """ # Get the Visualizer subclass from the string. visu = Visualizer.from_name(visu_name) # Try to export data to one of the formats supported by the visualizer # Use a temporary file (note "." + ext) for ext in visu.supported_extensions(): ext = "." + ext try: return self.export(ext, visu=visu)() except visu.Error: pass else: raise visu.Error("Don't know how to export data for %s" % visu_name)
def visualize(self, visu_name): """ Visualize data with visualizer. See :class:`Visualizer` for the list of applications and formats supported. """ visu = Visualizer.from_name(visu_name) # Try to export data to one of the formats supported by the visualizer # Use a temporary file (note "." + ext) for ext in visu.supported_extensions(): ext = "." + ext try: return self.export(ext, visu=visu) except visu.Error: pass else: raise visu.Error("Don't know how to export data for visualizer %s" % visu_name)
def visualize_ur2(self, appname="vesta"): """ Visualize :math:`|u(r)|^2|`. See :class:`Visualizer` for the list of applications and formats supported. """ # Get the Visualizer subclass from the string. visu = Visualizer.from_name(appname) # Try to export data to one of the formats supported by the visualizer # Use a temporary file (note "." + ext) for ext in visu.supported_extensions(): ext = "." + ext try: return self.export_ur2(ext, visu=visu) except visu.Error: pass else: raise visu.Error("Don't know how to export data for %s" % str(appname))
def visualize_ur2(self, structure, visu_name): """ Visualize u(r)**2 visualizer. See :class:`Visualizer` for the list of applications and formats supported. """ # Get the Visualizer subclass from the string. visu = Visualizer.from_name(visu_name) # Try to export data to one of the formats supported by the visualizer # Use a temporary file (note "." + ext) for ext in visu.supported_extensions(): ext = "." + ext try: return self.export_ur2(ext, structure, visu=visu) except visu.Error: pass else: raise visu.Error("Don't know how to export data for %s" % visu_name)
def ipw_visualize_widget(self): # pragma: no cover """ Return an ipython widget with controllers to visualize the wavefunctions. .. warning:: It seems there's a bug with Vesta on MacOs if the user tries to open multiple wavefunctions as the tab in vesta is not updated! """ def wfk_visualize(spin, kpoint, band, appname): kpoint = int(kpoint.split()[0]) self.visualize_ur2(spin, kpoint, band, appname=appname) import ipywidgets as ipw return ipw.interact_manual( wfk_visualize, spin=list(range(self.nsppol)), kpoint=["%d %s" % (i, repr(kpt)) for i, kpt in enumerate(self.kpoints)], band=list(range(self.nband)), appname=[v.name for v in Visualizer.get_available()], )