-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathstructure_3d.py
More file actions
212 lines (181 loc) · 6.75 KB
/
structure_3d.py
File metadata and controls
212 lines (181 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""Plotly 3D structure examples."""
# %%
from matminer.datasets import load_dataset
from pymatgen.core import Lattice, Structure
from pymatgen.core.periodic_table import Species
import pymatviz as pmv
from pymatviz.enums import ElemColorScheme, Key, SiteCoords
from pymatviz.structure import disordered_demo_structures
df_phonons = load_dataset("matbench_phonons")
# %% 3d example
n_structs = 6
supercells = {
key: struct.make_supercell(2, in_place=False)
for key, struct in df_phonons[Key.structure].head(n_structs).items()
}
fig = pmv.structure_3d(
supercells,
elem_colors=ElemColorScheme.jmol,
# show_cell={"edge": dict(color="white", width=1.5)},
hover_text=SiteCoords.cartesian_fractional,
show_bonds=True,
)
fig.layout.title = f"{n_structs} Matbench phonon structures (3D supercells)"
fig.show()
# pmv.io.save_and_compress_svg(fig, "matbench-phonons-structures-3d")
# %% BaTiO3 = https://materialsproject.org/materials/mp-5020
batio3 = Structure(
lattice=Lattice.cubic(4.0338),
species=["Ba", "Ti", "O", "O", "O"],
coords=[(0, 0, 0), (0.5, 0.5, 0.5), (0.5, 0.5, 0), (0.5, 0, 0.5), (0, 0.5, 0.5)],
)
# Add oxidation states to help with bond determination
batio3.add_oxidation_state_by_element({"Ba": 2, "Ti": 4, "O": -2})
# Demonstrate custom legend positioning and sizing
fig = pmv.structure_3d(
batio3, show_cell={"edge": dict(color="white", width=2)}, show_bonds=True
)
fig.show()
# pmv.io.save_and_compress_svg(fig, "bato3-structure-3d")
# %% Example: Disordered site rendering (multiple spheres in 3D)
fig = pmv.structure_3d(
disordered_demo_structures,
elem_colors=ElemColorScheme.jmol,
n_cols=2,
show_cell={"edge": dict(color="darkgray", width=2)},
site_labels="symbol",
hover_text=SiteCoords.cartesian_fractional,
)
fig.layout.title = dict(
text="3D Disordered Sites: Spherical Wedges Show Species Occupancy",
x=0.5,
font=dict(size=16),
)
# fig.layout.update(width=1200, height=800)
fig.show()
# pmv.io.save_and_compress_svg(fig, "disordered-sites-3d-spherical-wedges")
# %% Create a high-entropy alloy structure CoCrFeNiMn with FCC structure
lattice = Lattice.cubic(3.59)
hea_structure = Structure(
lattice=lattice,
species=["Co", "Cr", "Fe", "Ni", "Mn"],
coords=[(0, 0, 0), (0.5, 0.5, 0), (0.5, 0, 0.5), (0, 0.5, 0.5), (0.5, 0.5, 0.5)],
)
fig = pmv.structure_3d(
hea_structure.make_supercell([2, 3, 2], in_place=False),
show_cell={"edge": dict(color="white", width=2)},
)
title = "CoCrFeNiMn High-Entropy Alloy"
fig.layout.title = title
fig.show()
# pmv.io.save_and_compress_svg(fig, "hea-structure-3d")
# %% Li-ion battery cathode material with Li vacancies: Li0.8CoO2
lco_lattice = Lattice.hexagonal(2.82, 14.05)
lco_supercell = Structure(
lattice=lco_lattice,
species=[Species("Li", 0.8), "Co", "O", "O"], # Partially occupied Li site
coords=[(0, 0, 0), (0, 0, 0.5), (0, 0, 0.25), (0, 0, 0.75)],
).make_supercell([3, 3, 1])
fig = pmv.structure_3d(
lco_supercell,
show_cell={"edge": dict(color="white", width=1.5)},
elem_colors=ElemColorScheme.jmol,
)
title = "Li0.8CoO2 with Li Vacancies"
fig.layout.title = title
fig.show()
# pmv.io.save_and_compress_svg(fig, "lco-structure-3d")
# %% 2x2 Grid showcasing multiple customization options
# Structure 1: Diamond cubic silicon with vdW color scheme
si_diamond = Structure(
lattice=Lattice.cubic(5.43),
species=["Si"] * 8,
coords=[
(0, 0, 0),
(0.25, 0.25, 0.25),
(0.5, 0.5, 0),
(0.75, 0.75, 0.25),
(0.5, 0, 0.5),
(0.75, 0.25, 0.75),
(0, 0.5, 0.5),
(0.25, 0.75, 0.75),
],
)
# Structure 2: Perovskite CaTiO3 with CPK colors
catio3 = Structure(
lattice=Lattice.cubic(3.84),
species=["Ca", "Ti", "O", "O", "O"],
coords=[(0, 0, 0), (0.5, 0.5, 0.5), (0.5, 0.5, 0), (0.5, 0, 0.5), (0, 0.5, 0.5)],
)
catio3.add_oxidation_state_by_element({"Ca": 2, "Ti": 4, "O": -2})
# Structure 3: Zinc blende ZnS with VESTA colors
zns = Structure(
lattice=Lattice.cubic(5.41),
species=["Zn", "S", "Zn", "S"],
coords=[(0, 0, 0), (0.25, 0.25, 0.25), (0.5, 0.5, 0), (0.75, 0.75, 0.25)],
).make_supercell([2, 2, 1])
# Structure 4: Layered MoS2 with accessible colors
mos2_lattice = Lattice.hexagonal(3.16, 12.30)
mos2 = Structure(
lattice=mos2_lattice,
species=["Mo", "S", "S"],
coords=[(0, 0, 0.5), (0.333, 0.667, 0.375), (0.333, 0.667, 0.625)],
).make_supercell([2, 2, 2])
structures_grid = {
"Si Diamond (Jmol colors)": si_diamond,
"CaTiO₃ Perovskite (VESTA colors)": catio3,
"ZnS Zinc Blende (Alloy colors)": zns,
"MoS₂ Layered (Pastel colors)": mos2,
}
fig = pmv.structure_3d(
structures_grid,
elem_colors={ # different color schemes to showcase variety
"Si Diamond (Jmol colors)": ElemColorScheme.jmol,
"CaTiO₃ Perovskite (VESTA colors)": ElemColorScheme.vesta,
"ZnS Zinc Blende (Alloy colors)": ElemColorScheme.alloy,
"MoS₂ Layered (Pastel colors)": ElemColorScheme.pastel,
},
# Show cells with different styling for each subplot
show_cell={
"Si Diamond (Jmol colors)": {"edge": dict(color="red", width=2.5)},
"CaTiO₃ Perovskite (VESTA colors)": {"edge": dict(color="blue", width=2)},
"ZnS Zinc Blende (Alloy colors)": {"edge": dict(color="green", width=1.5)},
"MoS₂ Layered (Pastel colors)": {"edge": dict(color="purple", width=3)},
},
hover_text=SiteCoords.cartesian_fractional,
show_bonds={ # Show bonds for some structures but not others
"Si Diamond (Jmol colors)": True,
"CaTiO₃ Perovskite (VESTA colors)": True,
"ZnS Zinc Blende (Alloy colors)": False,
"MoS₂ Layered (Pastel colors)": True,
},
)
fig.update_layout(
title=dict(text="Kitchen Sink of Customization Options", x=0.5),
showlegend=True,
width=1200,
height=900,
)
fig.show()
# pmv.io.save_and_compress_svg(fig, "structures-2x2-grid-comprehensive-options")
# %% Simple cubic structure to show effect of cell_boundary_tol
LiF_cubic = Structure(
lattice=Lattice.cubic(3.0),
species=["Li", "F"],
coords=[(0, 0, 0), (0.5, 0.5, 0.5)],
)
# Show same structure with different cell_boundary_tol values
cell_boundary_tols = {"Strict boundaries (tol=0)": 0, "Loose (tol=0.5)": 0.5}
fig = pmv.structure_3d(
dict.fromkeys(cell_boundary_tols, LiF_cubic)
| {"Via properties": LiF_cubic.copy(properties={"cell_boundary_tol": 1})},
cell_boundary_tol=cell_boundary_tols,
show_image_sites=True,
show_sites=True,
site_labels="symbol",
)
title = "Effect of cell_boundary_tol on Image Site Rendering"
subtitle = "Higher tolerance values include more atoms outside the unit cell"
fig.layout.title = dict(text=f"{title}<br><sub>{subtitle}</sub>", x=0.5, font_size=16)
fig.layout.update(height=600, width=800, margin_t=50)
fig.show()