Skip to content
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

Use np.eye(3) instead of [[1, 0, 0], [0, 1, 0], [0, 0, 1]] for identies #3659

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ def setup_random_structure(self, coordination):
for _ in range(coordination + 1):
coords.append(aa * np.random.random_sample(3) + bb)
self.set_structure(
lattice=np.array([[10, 0, 0], [0, 10, 0], [0, 0, 10]], float),
lattice=np.array(np.eye(3) * 10, float),
species=["Si"] * (coordination + 1),
coords=coords,
coords_are_cartesian=False,
Expand Down
6 changes: 3 additions & 3 deletions pymatgen/analysis/dimensionality.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def get_dimensionality_cheon(
connected_list1 = find_connected_atoms(structure, tolerance=tolerance, ldict=ldict)
max1, min1, _clusters1 = find_clusters(structure, connected_list1)
if larger_cell:
structure.make_supercell([[3, 0, 0], [0, 3, 0], [0, 0, 3]])
structure.make_supercell(np.eye(3) * 3)
connected_list3 = find_connected_atoms(structure, tolerance=tolerance, ldict=ldict)
max3, min3, _clusters3 = find_clusters(structure, connected_list3)
if min3 == min1:
Expand All @@ -352,7 +352,7 @@ def get_dimensionality_cheon(
else:
return None
else:
structure.make_supercell([[2, 0, 0], [0, 2, 0], [0, 0, 2]])
structure.make_supercell(np.eye(3) * 2)
connected_list2 = find_connected_atoms(structure, tolerance=tolerance, ldict=ldict)
max2, min2, _clusters2 = find_clusters(structure, connected_list2)
if min2 == 1:
Expand All @@ -365,7 +365,7 @@ def get_dimensionality_cheon(
dim = str(int(dim)) + "D"
else:
structure = copy.copy(structure_save)
structure.make_supercell([[3, 0, 0], [0, 3, 0], [0, 0, 3]])
structure.make_supercell(np.eye(3) * 3)
connected_list3 = find_connected_atoms(structure, tolerance=tolerance, ldict=ldict)
max3, min3, _clusters3 = find_clusters(structure, connected_list3)
if min3 == min2:
Expand Down
28 changes: 10 additions & 18 deletions pymatgen/analysis/gb/grain.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,7 @@ def gb_from_parameters(
rotation_axis = [int(round(x / reduce(gcd, rotation_axis))) for x in rotation_axis]
# transform four index notation to three index notation for plane
if plane is not None and len(plane) == 4:
u1 = plane[0]
v1 = plane[1]
w1 = plane[3]
u1, v1, w1 = plane[0], plane[1], plane[3]
plane = [u1, v1, w1]
# set the plane for grain boundary when plane is None.
if plane is None:
Expand All @@ -550,16 +548,10 @@ def gb_from_parameters(
c2_a2_ratio = 1.0 if ratio is None else ratio[0] / ratio[1]
metric = np.array([[1, 0, 0], [0, 1, 0], [0, 0, c2_a2_ratio]])
elif lat_type.lower() == "o":
for i in range(3):
if ratio[i] is None:
ratio[i] = 1
metric = np.array(
[
[1, 0, 0],
[0, ratio[1] / ratio[2], 0],
[0, 0, ratio[0] / ratio[2]],
]
)
for idx in range(3):
if ratio[idx] is None:
ratio[idx] = 1
metric = np.array([[1, 0, 0], [0, ratio[1] / ratio[2], 0], [0, 0, ratio[0] / ratio[2]]])
else:
raise RuntimeError("Lattice type has not implemented.")

Expand Down Expand Up @@ -676,14 +668,14 @@ def gb_from_parameters(
index_incident = np.nonzero(t_and_b_dis < np.min(t_and_b_dis) + tol_coi)

top_labels = []
for i in range(n_sites):
if i in index_incident[0]:
for idx in range(n_sites):
if idx in index_incident[0]:
top_labels.append("top_incident")
else:
top_labels.append("top")
bottom_labels = []
for i in range(n_sites):
if i in index_incident[1]:
for idx in range(n_sites):
if idx in index_incident[1]:
bottom_labels.append("bottom_incident")
else:
bottom_labels.append("bottom")
Expand Down Expand Up @@ -2302,7 +2294,7 @@ def symm_group_cubic(mat):
cubic symmetric equivalents of the list of vectors.
"""
sym_group = np.zeros([24, 3, 3])
sym_group[0, :] = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
sym_group[0, :] = np.eye(3)
sym_group[1, :] = [[1, 0, 0], [0, -1, 0], [0, 0, -1]]
sym_group[2, :] = [[-1, 0, 0], [0, 1, 0], [0, 0, -1]]
sym_group[3, :] = [[-1, 0, 0], [0, -1, 0], [0, 0, 1]]
Expand Down
4 changes: 2 additions & 2 deletions pymatgen/core/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@ def get_niggli_reduced_lattice(self, tol: float = 1e-5) -> Lattice:
i = -1 if ll == -1 else 1
j = -1 if m == -1 else 1
k = -1 if n == -1 else 1
M = [[i, 0, 0], [0, j, 0], [0, 0, k]]
M = np.diag((i, j, k))
G = np.dot(np.transpose(M), np.dot(G, M))
elif ll * m * n in (0, -1):
# A4
Expand All @@ -1123,7 +1123,7 @@ def get_niggli_reduced_lattice(self, tol: float = 1e-5) -> Lattice:
j = -1
elif ll == 0:
i = -1
M = [[i, 0, 0], [0, j, 0], [0, 0, k]]
M = np.diag((i, j, k))
G = np.dot(np.transpose(M), np.dot(G, M))

A, B, C, E, N, Y = G[0, 0], G[1, 1], G[2, 2], 2 * G[1, 2], 2 * G[0, 2], 2 * G[0, 1]
Expand Down
2 changes: 1 addition & 1 deletion pymatgen/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -3699,7 +3699,7 @@ def __init__(
self,
lattice: ArrayLike | Lattice,
species: Sequence[CompositionLike],
coords: Sequence[ArrayLike],
coords: Sequence[ArrayLike] | np.ndarray,
charge: float | None = None,
validate_proximity: bool = False,
to_unit_cell: bool = False,
Expand Down
2 changes: 1 addition & 1 deletion pymatgen/electronic_structure/boltztrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def write_struct(self, output_file) -> None:
if self._symprec is not None:
ops = sym.get_symmetry_dataset()["rotations"]
elif self._symprec is None:
ops = [[[1, 0, 0], [0, 1, 0], [0, 0, 1]]]
ops = [np.eye(3)]
file.write(f"{len(ops)}\n")

for op in ops:
Expand Down
4 changes: 2 additions & 2 deletions pymatgen/ext/matproj_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1096,8 +1096,8 @@ def submit_snl(self, snl):
MPRestError
"""
snl = snl if isinstance(snl, list) else [snl]
jsondata = [s.as_dict() for s in snl]
payload = {"snl": json.dumps(jsondata, cls=MontyEncoder)}
json_data = [s.as_dict() for s in snl]
payload = {"snl": json.dumps(json_data, cls=MontyEncoder)}
response = self.session.post(f"{self.preamble}/snl/submit", data=payload)
if response.status_code in [200, 400]:
resp = json.loads(response.text, cls=MontyDecoder)
Expand Down
11 changes: 4 additions & 7 deletions pymatgen/symmetry/kpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,9 +995,9 @@ def _trans_sc_to_Hin(sub_class):
if sub_class == "oI3":
return np.array([[0, 0, 1], [1, 0, 0], [0, 1, 0]])
if sub_class == "oA2":
return np.array([[-1, 0, 0], [0, 1, 0], [0, 0, -1]])
return np.diag((-1, 1, -1))
if sub_class == "oC2":
return np.array([[-1, 0, 0], [0, 1, 0], [0, 0, -1]])
return np.diag((-1, 1, -1))
if sub_class in ["mP1", "mC1", "mC2", "mC3"]:
return np.array([[0, 1, 0], [-1, 0, 0], [0, 0, 1]])
raise RuntimeError("Sub-classification of crystal not found!")
Expand Down Expand Up @@ -1099,7 +1099,7 @@ def mag_type(self):
return self._mag_type

def _get_ksymm_kpath(self, has_magmoms, magmom_axis, axis_specified, symprec, angle_tolerance, atol):
ID = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
ID = np.eye(3)
# parity, aka the inversion operation (not calling it
PAR = np.array([[-1, 0, 0], [0, -1, 0], [0, 0, -1]])
# INV to avoid confusion with np.linalg.inv() function)
Expand Down Expand Up @@ -1319,11 +1319,8 @@ def _choose_path(
little_groups_points,
little_groups_lines,
):
#
# This function can be edited to alter high-symmetry criteria for choosing points and lines
#

ID = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
ID = np.eye(3)
PAR = np.array([[-1, 0, 0], [0, -1, 0], [0, 0, -1]])

gamma_ind = len(key_points) - 1
Expand Down
2 changes: 1 addition & 1 deletion pymatgen/vis/structure_vtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def __init__(
self.poly_radii_tol_factor = poly_radii_tol_factor
self.excluded_bonding_elements = excluded_bonding_elements or []
self.show_help = True
self.supercell = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
self.supercell = np.eye(3)
self.redraw()

style = StructureInteractorStyle(self)
Expand Down
16 changes: 8 additions & 8 deletions tests/analysis/test_local_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
class TestValenceIonicRadiusEvaluator(PymatgenTest):
def setUp(self):
"""Setup MgO rocksalt structure for testing Vacancy."""
mgo_latt = [[4.212, 0, 0], [0, 4.212, 0], [0, 0, 4.212]]
mgo_latt = np.eye(3) * 4.212
mgo_specie = ["Mg"] * 4 + ["O"] * 4
mgo_frac_cord = [
[0, 0, 0],
Expand All @@ -63,12 +63,12 @@ def setUp(self):

def test_valences_ionic_structure(self):
valence_dict = self._mgo_val_rad_evaluator.valences
for val in list(valence_dict.values()):
for val in valence_dict.values():
assert val in {2, -2}

def test_radii_ionic_structure(self):
radii_dict = self._mgo_val_rad_evaluator.radii
for rad in list(radii_dict.values()):
for rad in radii_dict.values():
assert rad in {0.86, 1.26}


Expand Down Expand Up @@ -112,7 +112,7 @@ def test_solid_angle(self):

def test_nn_shell(self):
# First, make a SC lattice. Make my math easier
struct = Structure([[1, 0, 0], [0, 1, 0], [0, 0, 1]], ["Cu"], [[0, 0, 0]])
struct = Structure(np.eye(3), ["Cu"], [[0, 0, 0]])

# Get the 1NN shell
self.nn.targets = None
Expand Down Expand Up @@ -155,7 +155,7 @@ def test_nn_shell(self):

def test_adj_neighbors(self):
# Make a simple cubic structure
struct = Structure([[1, 0, 0], [0, 1, 0], [0, 0, 1]], ["Cu"], [[0, 0, 0]])
struct = Structure(np.eye(3), ["Cu"], [[0, 0, 0]])

# Compute the NNs with adjacency
self.nn.targets = None
Expand Down Expand Up @@ -217,7 +217,7 @@ def test_filtered(self):

# Make a bcc crystal
bcc = Structure(
[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
np.eye(3),
["Cu", "Cu"],
[[0, 0, 0], [0.5, 0.5, 0.5]],
coords_are_cartesian=False,
Expand Down Expand Up @@ -509,7 +509,7 @@ def setUp(self):
site_properties=None,
)
self.square_pyramid = Structure(
Lattice([[100, 0, 0], [0, 100, 0], [0, 0, 100]]),
Lattice(np.eye(3) * 100),
["C", "C", "C", "C", "C", "C"],
[[0, 0, 0], [1, 0, 0], [-1, 0, 0], [0, 1, 0], [0, -1, 0], [0, 0, 1]],
validate_proximity=False,
Expand All @@ -518,7 +518,7 @@ def setUp(self):
site_properties=None,
)
self.trigonal_bipyramid = Structure(
Lattice([[100, 0, 0], [0, 100, 0], [0, 0, 100]]),
Lattice(np.eye(3) * 100),
["P", "Cl", "Cl", "Cl", "Cl", "Cl"],
[
[0, 0, 0],
Expand Down
4 changes: 2 additions & 2 deletions tests/analysis/test_structure_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,8 +788,8 @@ def test_rms_vs_minimax(self):
latt = Lattice.orthorhombic(1, 2, 12)

sp = ["Si", "Si", "Al"]
s1 = Structure(latt, sp, [[0.5, 0, 0], [0, 0, 0], [0, 0, 0.5]])
s2 = Structure(latt, sp, [[0.5, 0, 0], [0, 0, 0], [0, 0, 0.6]])
s1 = Structure(latt, sp, np.diag((0.5, 0, 0.5)))
s2 = Structure(latt, sp, np.diag((0.5, 0, 0.6)))
assert_allclose(sm.get_rms_dist(s1, s2), (0.32**0.5 / 2, 0.4))

assert sm.fit(s1, s2) is False
Expand Down
23 changes: 12 additions & 11 deletions tests/command_line/test_enumlib_caller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import unittest
from shutil import which

import numpy as np
import pytest
from pytest import approx

Expand All @@ -22,46 +23,46 @@
class TestEnumlibAdaptor(PymatgenTest):
def test_init(self):
struct = self.get_structure("LiFePO4")
subtrans = SubstitutionTransformation({"Li": {"Li": 0.5}})
adaptor = EnumlibAdaptor(subtrans.apply_transformation(struct), 1, 2)
sub_trans = SubstitutionTransformation({"Li": {"Li": 0.5}})
adaptor = EnumlibAdaptor(sub_trans.apply_transformation(struct), 1, 2)
adaptor.run()
structures = adaptor.structures
assert len(structures) == 86
for s in structures:
assert s.composition.get_atomic_fraction(Element("Li")) == approx(0.5 / 6.5)
adaptor = EnumlibAdaptor(subtrans.apply_transformation(struct), 1, 2, refine_structure=True)
adaptor = EnumlibAdaptor(sub_trans.apply_transformation(struct), 1, 2, refine_structure=True)
adaptor.run()
structures = adaptor.structures
assert len(structures) == 52

subtrans = SubstitutionTransformation({"Li": {"Li": 0.25}})
adaptor = EnumlibAdaptor(subtrans.apply_transformation(struct), 1, 1, refine_structure=True)
sub_trans = SubstitutionTransformation({"Li": {"Li": 0.25}})
adaptor = EnumlibAdaptor(sub_trans.apply_transformation(struct), 1, 1, refine_structure=True)
adaptor.run()
structures = adaptor.structures
assert len(structures) == 1
for s in structures:
assert s.composition.get_atomic_fraction(Element("Li")) == approx(0.25 / 6.25)

# Make sure it works for completely disordered structures.
struct = Structure([[10, 0, 0], [0, 10, 0], [0, 0, 10]], [{"Fe": 0.5}], [[0, 0, 0]])
struct = Structure(np.eye(3) * 10, [{"Fe": 0.5}], [[0, 0, 0]])
adaptor = EnumlibAdaptor(struct, 1, 2)
adaptor.run()
assert len(adaptor.structures) == 3

# Make sure it works properly when symmetry is broken by ordered sites.
struct = self.get_structure("LiFePO4")
subtrans = SubstitutionTransformation({"Li": {"Li": 0.25}})
s = subtrans.apply_transformation(struct)
sub_trans = SubstitutionTransformation({"Li": {"Li": 0.25}})
s = sub_trans.apply_transformation(struct)
# REmove some ordered sites to break symmetry.
removetrans = RemoveSitesTransformation([4, 7])
s = removetrans.apply_transformation(s)
remove_trans = RemoveSitesTransformation([4, 7])
s = remove_trans.apply_transformation(s)
adaptor = EnumlibAdaptor(s, 1, 1, enum_precision_parameter=0.01)
adaptor.run()
structures = adaptor.structures
assert len(structures) == 4

struct = Structure(
[[3, 0, 0], [0, 3, 0], [0, 0, 3]],
np.eye(3) * 3,
[{"Si": 0.5}] * 2,
[[0, 0, 0], [0.5, 0.5, 0.5]],
)
Expand Down
11 changes: 6 additions & 5 deletions tests/command_line/test_gulp_caller.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import unittest
from shutil import which

import numpy as np
import pytest

from pymatgen.analysis.bond_valence import BVAnalyzer
Expand All @@ -35,7 +36,7 @@
@unittest.skipIf(not gulp_present, "gulp not present.")
class TestGulpCaller(unittest.TestCase):
def test_run(self):
mgo_lattice = [[4.212, 0, 0], [0, 4.212, 0], [0, 0, 4.212]]
mgo_lattice = np.eye(3) * 4.212
mgo_specie = ["Mg"] * 4 + ["O"] * 4
mgo_frac_cord = [
[0, 0, 0],
Expand Down Expand Up @@ -136,7 +137,7 @@ def test_library_line_wrong_file(self):
self.gio.library_line("temp_to_fail.lib")

def test_buckingham_potential(self):
mgo_latt = [[4.212, 0, 0], [0, 4.212, 0], [0, 0, 4.212]]
mgo_latt = np.eye(3) * 4.212
mgo_specie = ["Mg", "O"] * 4
mgo_frac_cord = [
[0, 0, 0],
Expand All @@ -163,7 +164,7 @@ def test_buckingham_potential(self):
assert "spring" in gin

def test_buckingham_input(self):
mgo_latt = [[4.212, 0, 0], [0, 4.212, 0], [0, 0, 4.212]]
mgo_latt = np.eye(3) * 4.212
mgo_specie = ["Mg", "O"] * 4
mgo_frac_cord = [
[0, 0, 0],
Expand All @@ -188,7 +189,7 @@ def test_buckingham_input(self):

# Improve the test
def test_tersoff_potential(self):
mgo_latt = [[4.212, 0, 0], [0, 4.212, 0], [0, 0, 4.212]]
mgo_latt = np.eye(3) * 4.212
mgo_specie = ["Mg", "O"] * 4
mgo_frac_cord = [
[0, 0, 0],
Expand Down Expand Up @@ -260,7 +261,7 @@ def test_tersoff_input(self):
@unittest.skipIf(not gulp_present, "gulp not present.")
class TestGlobalFunctions(unittest.TestCase):
def setUp(self):
mgo_latt = [[4.212, 0, 0], [0, 4.212, 0], [0, 0, 4.212]]
mgo_latt = np.eye(3) * 4.212
mgo_specie = ["Mg", "O"] * 4
mgo_frac_cord = [
[0, 0, 0],
Expand Down