WavefunctionPlot
The WavefunctionPlot
class will help you very easily generate and display wavefunctions from a Hamiltonian
or any other source. If you already have your wavefunction in a grid, you can use GridPlot
.
Note
WavefunctionPlot
is just an extension of GridPlot
, so everything in the GridPlot notebook applies and this notebook will only display the additional features.
[1]:
import sisl
import sisl.viz
Generating wavefunctions from a hamiltonian
We will create a toy graphene tight binding hamiltonian, but you could have read the Hamiltonian
from any source. Note that your hamiltonian needs to contain the corresponding geometry with the right orbitals, otherwise we have no idea what’s the shape of the wavefunction.
[2]:
import numpy as np
r = np.linspace(0, 3.5, 50)
f = np.exp(-r)
orb = sisl.AtomicOrbital('2pzZ', (r, f))
geom = sisl.geom.graphene(orthogonal=True, atoms=sisl.Atom(6, orb))
geom = geom.move([0, 0, 5])
H = sisl.Hamiltonian(geom)
H.construct([(0.1, 1.44), (0, -2.7)], )
Now that we have our hamiltonian, plotting a wavefunction is as simple as:
[3]:
H.plot.wavefunction()
That truly is an ugly wavefunction.
Selecting the wavefunction
By default, WavefunctionPlot
gives you the first wavefunction at the gamma point. You can control this behavior by tuning the i
and k
settings.
For example, to get the second wavefunction at the gamma point:
[4]:
plot = H.plot.wavefunction(i=2, k=(0, 0, 0))
plot
You can also select the spin with the spin
setting (if you have, of course, a spin polarized Hamiltonian
).
Note
If you update the number of the wavefunction, the eigenstates are already calculated, so there’s no need to recalculate them. However, changing the k point or the spin component will trigger a recalculation of the eigenstates.
Grid precision
The wavefunction is projected in a grid, and how fine that grid is will determine the resolution. You can control this with the grid_prec
setting, which accepts the grid precision in Angstrom. Let’s check the difference in 2D, where it will be best appreciated:
[5]:
plot.update_settings(axes="xy", k=(0,0,0), transforms=["square"]) # by default grid_prec is 0.2 Ang
[6]:
plot.update_settings(grid_prec=0.05)
Much better, isn’t it? Notice how it didn’t look that bad in 3d, because the grid is smooth, so it’s values are nicely interpolated. You can also appreciate this by setting zsmooth
to "best"
in 2D, which does an “OK job” at guessing the values.
[7]:
plot.update_settings(grid_prec=0.2, zsmooth="best")
Warning
Keep in mind that a finer grid will occupy more memory and take more time to generate and render, and sometimes it might be unnecessary to make your grid very fine, specially if it’s smooth.
GridPlot settings
As stated at the beggining of this notebook, you have all the power of GridPlot
available to you. Therefore you can, for example, display supercells of the resulting wavefunctions (please don’t tile the hamiltonian! :)).
[8]:
plot.update_settings(axes="xyz", nsc=[2,2,1], grid_prec=0.1, transforms=[],
isos=[
{"val": -0.07, "opacity": 1, "color": "salmon"},
{"val": 0.07, "opacity": 0.7, "color": "blue"}
],
geom_kwargs={"atoms_style": dict(color=["orange", "red", "green", "pink"])},
)
We hope you enjoyed what you learned!
This next cell is just to create the thumbnail for the notebook in the docs
[9]:
thumbnail_plot = plot
if thumbnail_plot:
thumbnail_plot.show("png")
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [9], in <cell line: 3>()
1 thumbnail_plot = plot
3 if thumbnail_plot:
----> 4 thumbnail_plot.show("png")
File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:1176, in Plot.show(self, listen, return_figWidget, *args, **kwargs)
1173 except Exception as e:
1174 warn(e)
-> 1176 return self._backend.show(*args, **kwargs)
File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/backends/plotly/backend.py:42, in PlotlyBackend.show(self, *args, **kwargs)
41 def show(self, *args, **kwargs):
---> 42 return self.figure.show(*args, **kwargs)
File ~/checkouts/readthedocs.org/user_builds/sisl/conda/v0.12.2/lib/python3.10/site-packages/plotly/basedatatypes.py:3398, in BaseFigure.show(self, *args, **kwargs)
3365 """
3366 Show a figure using either the default renderer(s) or the renderer(s)
3367 specified by the renderer argument
(...)
3394 None
3395 """
3396 import plotly.io as pio
-> 3398 return pio.show(self, *args, **kwargs)
File ~/checkouts/readthedocs.org/user_builds/sisl/conda/v0.12.2/lib/python3.10/site-packages/plotly/io/_renderers.py:388, in show(fig, renderer, validate, **kwargs)
385 fig_dict = validate_coerce_fig_to_dict(fig, validate)
387 # Mimetype renderers
--> 388 bundle = renderers._build_mime_bundle(fig_dict, renderers_string=renderer, **kwargs)
389 if bundle:
390 if not ipython_display:
File ~/checkouts/readthedocs.org/user_builds/sisl/conda/v0.12.2/lib/python3.10/site-packages/plotly/io/_renderers.py:296, in RenderersConfig._build_mime_bundle(self, fig_dict, renderers_string, **kwargs)
293 if hasattr(renderer, k):
294 setattr(renderer, k, v)
--> 296 bundle.update(renderer.to_mimebundle(fig_dict))
298 return bundle
File ~/checkouts/readthedocs.org/user_builds/sisl/conda/v0.12.2/lib/python3.10/site-packages/plotly/io/_base_renderers.py:127, in ImageRenderer.to_mimebundle(self, fig_dict)
126 def to_mimebundle(self, fig_dict):
--> 127 image_bytes = to_image(
128 fig_dict,
129 format=self.format,
130 width=self.width,
131 height=self.height,
132 scale=self.scale,
133 validate=False,
134 engine=self.engine,
135 )
137 if self.b64_encode:
138 image_str = base64.b64encode(image_bytes).decode("utf8")
File ~/checkouts/readthedocs.org/user_builds/sisl/conda/v0.12.2/lib/python3.10/site-packages/plotly/io/_kaleido.py:133, in to_image(fig, format, width, height, scale, validate, engine)
131 # Raise informative error message if Kaleido is not installed
132 if scope is None:
--> 133 raise ValueError(
134 """
135 Image export using the "kaleido" engine requires the kaleido package,
136 which can be installed using pip:
137 $ pip install -U kaleido
138 """
139 )
141 # Validate figure
142 # ---------------
143 fig_dict = validate_coerce_fig_to_dict(fig, validate)
ValueError:
Image export using the "kaleido" engine requires the kaleido package,
which can be installed using pip:
$ pip install -U kaleido