-
Notifications
You must be signed in to change notification settings - Fork 25
Exciton wavefunction #180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Exciton wavefunction #180
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dd62f45
Read and plot exciton charge density
46c0113
Clean up
384e1a5
Shift the cell if view.center=True
14a4f33
Fix typos. Get rid of redundant _read_density
7e4ac2b
Don't need to pass shift as an argument
25c9ef7
Make shifts before replication.
4698873
Rename exciton to exciton_density
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
# Copyright © VASP Software GmbH, | ||
# Licensed under the Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
import numpy as np | ||
|
||
from py4vasp import _config, calculation, exception | ||
from py4vasp._third_party import view | ||
from py4vasp._util import documentation, import_, index, select | ||
from py4vasp.calculation import _base, _structure | ||
|
||
pretty = import_.optional("IPython.lib.pretty") | ||
|
||
|
||
_INTERNAL = "1" | ||
|
||
class ExcitonDensity(_base.Refinery, _structure.Mixin, view.Mixin): | ||
"""This class accesses exciton charge densities of VASP. | ||
|
||
The exciton charge densities can be calculated via the BSE/TDHF algorithm in | ||
VASP. With this class you can extract these charge densities. | ||
""" | ||
|
||
@_base.data_access | ||
def __str__(self): | ||
_raise_error_if_no_data(self._raw_data.exciton_charge) | ||
grid = self._raw_data.exciton_charge.shape[1:] | ||
excitons = self._raw_data.exciton_charge.shape[0] | ||
topology = calculation.topology.from_data(self._raw_data.structure.topology) | ||
return f"""exciton charge density: | ||
structure: {pretty.pretty(topology)} | ||
grid: {grid[2]}, {grid[1]}, {grid[0]} | ||
excitons : {excitons}""" | ||
|
||
@_base.data_access | ||
def to_dict(self): | ||
"""Read the exciton density into a dictionary. | ||
|
||
Returns | ||
------- | ||
dict | ||
Contains the supercell structure information as well as the exciton | ||
charge density represented on a grid in the supercell. | ||
""" | ||
_raise_error_if_no_data(self._raw_data.exciton_charge) | ||
result = {"structure": self._structure.read()} | ||
result.update({"charge": self.to_numpy()}) | ||
return result | ||
|
||
@_base.data_access | ||
def to_numpy(self): | ||
"""Convert the exciton charge density to a numpy array. | ||
|
||
Returns | ||
------- | ||
np.ndarray | ||
Charge density of all excitons. | ||
""" | ||
return np.moveaxis(self._raw_data.exciton_charge, 0, -1).T | ||
|
||
@_base.data_access | ||
def to_view(self, selection=None, supercell=None, center=False, **user_options): | ||
"""Plot the selected exciton density as a 3d isosurface within the structure. | ||
|
||
Parameters | ||
---------- | ||
selection : str | ||
Can be exciton index or a combination, i.e., "1" or "1+2+3" | ||
|
||
supercell : int or np.ndarray | ||
If present the data is replicated the specified number of times along each | ||
direction. | ||
|
||
user_options | ||
Further arguments with keyword that get directly passed on to the | ||
visualizer. Most importantly, you can set isolevel to adjust the | ||
value at which the isosurface is drawn. | ||
|
||
Returns | ||
------- | ||
View | ||
Visualize an isosurface of the exciton density within the 3d structure. | ||
|
||
Examples | ||
-------- | ||
>>> calc = py4vasp.Calculation.from_path(".") | ||
Plot an isosurface of the first exciton charge density | ||
>>> calc.exciton.density.plot() | ||
Plot an isosurface of the third exciton charge density | ||
>>> calc.exciton.density.plot("3") | ||
Plot an isosurface of the sum of first and second exciton charge | ||
densities | ||
>>> calc.exciton.density.plot("1+2") | ||
""" | ||
_raise_error_if_no_data(self._raw_data.exciton_charge) | ||
selection = selection or _INTERNAL | ||
viewer = self._structure.plot(supercell) | ||
map_ = self._create_map() | ||
selector = index.Selector({0: map_}, self._raw_data.exciton_charge) | ||
tree = select.Tree.from_selection(selection) | ||
selections = tree.selections() | ||
viewer.grid_scalars = [ | ||
self._grid_quantity(selector, selection, map_, user_options) | ||
for selection in selections | ||
] | ||
if center: | ||
viewer.shift = (0.5,0.5,0.5) | ||
return viewer | ||
|
||
def _create_map(self): | ||
excitons=self._raw_data.exciton_charge.shape[0] | ||
map_ = { | ||
str(choice): choice-1 | ||
for choice in range(1,excitons+1) | ||
} | ||
return map_ | ||
|
||
def _grid_quantity(self, selector, selection, map_, user_options): | ||
component_label = selector.label(selection) | ||
return view.GridQuantity( | ||
quantity=(selector[selection].T)[np.newaxis], | ||
label=self._label(component_label), | ||
isosurfaces=self._isosurfaces(**user_options), | ||
) | ||
|
||
def _label(self, component_label): | ||
if self._selection: | ||
return f"{self._selection}({component_label})" | ||
else: | ||
return component_label | ||
|
||
def _isosurfaces(self, isolevel=0.8, color=None, opacity=0.6): | ||
color = color or _config.VASP_COLORS["cyan"] | ||
return [view.Isosurface(isolevel, color, opacity)] | ||
|
||
|
||
def _raise_error_if_no_data(data): | ||
if data.is_none(): | ||
raise exception.NoData( | ||
"Exciton charge density was not found. Note that the exciton density is" | ||
"written to vaspout.h5 if the tags LCHARGH5=T or LH5=T are set in" | ||
"the INCAR file" | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.