-
Notifications
You must be signed in to change notification settings - Fork 2
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
adding tools for plotting with py3dmol #109
Merged
Merged
Changes from all commits
Commits
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 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 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,42 @@ | ||
# Copyright (c) 2023 Graphcore Ltd. All rights reserved. | ||
from typing import Tuple | ||
|
||
import numpy as np | ||
from periodictable import elements | ||
from pyscf import gto | ||
|
||
from .basis import Basis, basisset | ||
from .structure import Structure | ||
|
||
|
||
def to_pyscf( | ||
structure: Structure, basis_name: str = "sto-3g", unit: str = "Bohr" | ||
) -> "gto.Mole": | ||
mol = gto.Mole(unit=unit, spin=structure.num_electrons % 2, cart=True) | ||
mol.atom = [ | ||
(symbol, pos) | ||
for symbol, pos in zip(structure.atomic_symbol, structure.position) | ||
] | ||
mol.basis = basis_name | ||
mol.build(unit=unit) | ||
return mol | ||
|
||
|
||
def from_pyscf(mol: "gto.Mole") -> Tuple[Structure, Basis]: | ||
atomic_number = [] | ||
position = [] | ||
|
||
for i in range(mol.natm): | ||
sym, pos = mol.atom[i] | ||
atomic_number.append(elements.symbol(sym).number) | ||
position.append(pos) | ||
|
||
structure = Structure( | ||
atomic_number=np.array(atomic_number), | ||
position=np.array(position), | ||
is_bohr=mol.unit != "Angstom", | ||
) | ||
|
||
basis = basisset(structure, basis_name=mol.basis) | ||
|
||
return structure, basis |
This file contains 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 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,79 @@ | ||
# Copyright (c) 2023 Graphcore Ltd. All rights reserved. | ||
import numpy as np | ||
from numpy.typing import NDArray | ||
|
||
from .structure import Structure | ||
from .types import MeshAxes | ||
from .units import to_angstrom | ||
|
||
|
||
def plot_volume(structure: Structure, value: NDArray, axes: MeshAxes): | ||
"""plots volumetric data value with molecular structure. | ||
|
||
Args: | ||
structure (Structure): molecular structure | ||
value (NDArray): the volume data to render | ||
axes (MeshAxes): the axes over which the data was sampled. | ||
|
||
Returns: | ||
py3DMol View object | ||
""" | ||
v = structure.view() | ||
v.addVolumetricData(cube_data(value, axes), "cube", build_transferfn(value)) | ||
return v | ||
|
||
|
||
def cube_data(value: NDArray, axes: MeshAxes) -> str: | ||
"""Generate the cube file format as a string. See: | ||
|
||
https://paulbourke.net/dataformats/cube/ | ||
|
||
Args: | ||
value (NDArray): the volume data to serialise in the cube format | ||
axes (MeshAxes): the axes over which the data was sampled | ||
|
||
Returns: | ||
str: cube format representation of the volumetric data. | ||
""" | ||
axes = [to_angstrom(ax) for ax in axes] | ||
fmt = "cube format\n\n" | ||
x, y, z = axes | ||
nx, ny, nz = [ax.shape[0] for ax in axes] | ||
fmt += "0 " + " ".join([f"{v:12.6f}" for v in [x[0], y[0], z[0]]]) + "\n" | ||
fmt += f"{nx} " + " ".join([f"{v:12.6f}" for v in [x[1] - x[0], 0.0, 0.0]]) + "\n" | ||
fmt += f"{ny} " + " ".join([f"{v:12.6f}" for v in [0.0, y[1] - y[0], 0.0]]) + "\n" | ||
fmt += f"{nz} " + " ".join([f"{v:12.6f}" for v in [0.0, 0.0, z[1] - z[0]]]) + "\n" | ||
|
||
line = "" | ||
for i in range(len(value)): | ||
line += f"{value[i]:12.6f}" | ||
|
||
if i % 6 == 0: | ||
fmt += line + "\n" | ||
line = "" | ||
|
||
return fmt | ||
|
||
|
||
def build_transferfn(value: NDArray) -> dict: | ||
"""Generate the 3dmol.js transferfn argument for a particular value. | ||
|
||
Tries to set isovalues to capture main features of the volume data. | ||
|
||
Args: | ||
value (NDArray): the volume data. | ||
|
||
Returns: | ||
dict: containing transferfn | ||
""" | ||
v = np.percentile(value, [99.9, 75]) | ||
a = [0.02, 0.0005] | ||
return { | ||
"transferfn": [ | ||
{"color": "blue", "opacity": a[0], "value": -v[0]}, | ||
{"color": "blue", "opacity": a[1], "value": -v[1]}, | ||
{"color": "white", "opacity": 0.0, "value": 0.0}, | ||
{"color": "red", "opacity": a[1], "value": v[1]}, | ||
{"color": "red", "opacity": a[0], "value": v[0]}, | ||
] | ||
} |
This file contains 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this be used somewhere else? Otherwise I would not pull it out - it forces the reader to jump around in the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was planning on using it in the nanoDFT notebook but then realised I would need to #92 implemented to do that.
I do think it makes sense to have separate API entry points
electron_density
andmolecular_orbitals
since both are useful to have (even if one is implemented in terms of the other).