Skip to content

Commit d4a100a

Browse files
Merge pull request #117 from SSCHAcode/spglib_fix
Fixed the spglib symmetrization to work with spglib beyond 2.2
2 parents 4045be7 + 62649f2 commit d4a100a

13 files changed

Lines changed: 56 additions & 26 deletions

File tree

UserGuide/gettingstarted.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,20 +309,20 @@ This tutorial requires spglib and the ASE packages installed.
309309
struct.has_unit_cell = True # periodic boundary conditions on
310310
311311
# Lets see the symmetries that prints spglib
312-
print("Original space group: ", spglib.get_spacegroup(struct.get_ase_atoms()))
312+
print("Original space group: ", spglib.get_spacegroup(struct.get_spglib_cell()))
313313
314314
# The previous command should print
315315
# Original space group: Im-3m (299)
316316
317317
# Lets store the symmetries and convert from spglib to the CellConstructor
318-
syms = spglib.get_symmetry(struct.get_ase_atoms())
318+
syms = spglib.get_symmetry(struct.get_spglib_cell())
319319
cc_syms = CC.symmetries.GetSymmetriesFromSPGLIB(syms)
320320
321321
# We can add a random noise on the atoms
322322
struct.coords += np.random.normal(0, 0.01, size = (2, 3))
323323
324324
# Let us print again the symmetry group
325-
print("Space group with noise: ", spglib.get_spacegroup(struct.get_ase_atoms()))
325+
print("Space group with noise: ", spglib.get_spacegroup(struct.get_spglib_cell()))
326326
327327
# This time the code will print
328328
# Space group with noise: P-1 (2)
@@ -333,7 +333,7 @@ This tutorial requires spglib and the ASE packages installed.
333333
struct.impose_symmetries(cc_syms)
334334
335335
# The previous command will print details on the symmetrization iterations
336-
print("Final group: ", spglib.get_spacegroup(struct.get_ase_atoms()))
336+
print("Final group: ", spglib.get_spacegroup(struct.get_spglib_cell()))
337337
# Now the structure will be again in the Im-3m group.
338338
339339
You can pass to all spglib commands a threshold for the symmetrization. In this case you can also use a large threshold and get the symmetries of the closest larger space group. You can use them to constrain the symmetries.

cellconstructor/Phonons.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,7 @@ def load_phonopy(self, yaml_filename = "phonopy.yaml", fc_filename = None):
13711371
It needs two files: the file with the structure information,
13721372
and the file with the force constant matrix.
13731373
1374-
TODO: Test properly, possible bugs.
1374+
TODO: Not working!!
13751375
13761376
Parameters
13771377
----------
@@ -3214,7 +3214,7 @@ def SymmetrizeSupercell(self, supercell_size = None):
32143214
#qe_sym.SetupQPoint()
32153215
qe_sym.ApplySymmetriesToV2(superdyn.dynmats[0])
32163216

3217-
#spgsym = spglib.get_symmetry(superdyn.structure.get_ase_atoms())
3217+
#spgsym = spglib.get_symmetry(superdyn.structure.get_spglib_cell())
32183218
#syms = symmetries.GetSymmetriesFromSPGLIB(spgsym, False)
32193219
#superdyn.ForceSymmetries(syms)
32203220

@@ -4804,7 +4804,7 @@ def compute_phonons_finite_displacements_sym(structure, ase_calculator, epsilon=
48044804

48054805
#print("DEBUG:", debug)
48064806
# Use spglib to get all the symmetry operations
4807-
symm = spglib.get_symmetry(super_structure.get_ase_atoms())
4807+
symm = spglib.get_symmetry(super_structure.get_spglib_cell())
48084808
symm = symmetries.GetSymmetriesFromSPGLIB(symm)
48094809
n_syms = len(symm)
48104810

cellconstructor/Structure.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,36 @@ def get_ase_atoms(self):
14681468

14691469
return atm
14701470

1471+
1472+
def get_spglib_cell(self):
1473+
"""
1474+
Convert the current structure in a valid spglib cell
1475+
for computing the symmetries.
1476+
1477+
The spglib cell is a standard tuple containing lattice, positions and
1478+
atomic numbers.
1479+
1480+
Results
1481+
-------
1482+
1483+
cell : Tuple
1484+
the standard tuple containing (lattice, positions, numbers) for
1485+
spglib.
1486+
1487+
"""
1488+
1489+
lattice = np.copy(self.unit_cell)
1490+
1491+
# Positions needs to be in fractional atomic units
1492+
positions = Methods.covariant_coordinates(lattice, self.coords)
1493+
1494+
# Numbers: convert atomic labels into integers
1495+
mapping = {}
1496+
numbers = [mapping.setdefault(s, len(mapping) + 1) for s in self.atoms]
1497+
1498+
cell = (lattice, positions, numbers)
1499+
return cell
1500+
14711501
def get_phonopy_calculation(self, supercell = [1,1,1]):
14721502
"""
14731503
Convert the CellConstructor structure to a phonopy object

cellconstructor/symmetries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,7 @@ def SetupFromSPGLIB(self):
12771277
raise ImportError("Error, this function works only if spglib is available")
12781278

12791279
# Get the symmetries
1280-
spg_syms = spglib.get_symmetry(self.structure.get_ase_atoms(), symprec = self.threshold)
1280+
spg_syms = spglib.get_symmetry(self.structure.get_spglib_cell(), symprec = self.threshold)
12811281
symmetries = GetSymmetriesFromSPGLIB(spg_syms, regolarize= False)
12821282

12831283
trans_irt = 0

tests/TestDiagonalizeSymmetries/test_diagsymmetries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_diag_symmetries():
2929

3030
# Get the symmetries
3131
supercell_s = dyn.structure.generate_supercell(dyn.GetSupercell())
32-
spglib_syms = spglib.get_symmetry(dyn.structure.get_ase_atoms())
32+
spglib_syms = spglib.get_symmetry(dyn.structure.get_spglib_cell())
3333
syms = CC.symmetries.GetSymmetriesFromSPGLIB(spglib_syms)
3434

3535
# Get the symmetries on the polarization vectors

tests/TestImposeSymmetries/test_impose_symmetries.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ def test_impose_symmetry():
3232
dyn = CC.Phonons.Phonons("old_dyn", full_name=True)
3333

3434
# Print the symmetry group at high threshold
35-
GROUP = spglib.get_spacegroup(dyn.structure.get_ase_atoms(), 0.05)
36-
s_group_expected = spglib.get_spacegroup(dyn.structure.get_ase_atoms())
35+
GROUP = spglib.get_spacegroup(dyn.structure.get_spglib_cell(), 0.05)
36+
s_group_expected = spglib.get_spacegroup(dyn.structure.get_spglib_cell())
3737
print ("Space group with high threshold:", s_group_expected)
3838
print ("Space group with low threshold:", GROUP)
3939

4040
# Get the symmetries from the new spacegroup
41-
symmetries = spglib.get_symmetry(dyn.structure.get_ase_atoms(), symprec = 0.05)
41+
symmetries = spglib.get_symmetry(dyn.structure.get_spglib_cell(), symprec = 0.05)
4242
print("Number of symmetries: {}".format(len(symmetries["rotations"])))
4343

4444
# Transform the spglib symmetries into the CellConstructor data type
@@ -47,7 +47,7 @@ def test_impose_symmetry():
4747
dyn.structure.impose_symmetries(sym_mats)
4848

4949
# Check once again the symetry
50-
s_group_after = spglib.get_spacegroup(dyn.structure.get_ase_atoms())
50+
s_group_after = spglib.get_spacegroup(dyn.structure.get_spglib_cell())
5151
print ("New space group with high threshold:", s_group_after)
5252

5353
assert s_group_after == GROUP

tests/TestModeSymmetries/test_double_symmetrization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_double_symmetrization(verbose = False):
3131
nat = ss.N_atoms
3232

3333
# Get the simmetries
34-
spglib_syms = spglib.get_symmetry(ss.get_ase_atoms())
34+
spglib_syms = spglib.get_symmetry(ss.get_spglib_cell())
3535
syms = CC.symmetries.GetSymmetriesFromSPGLIB(spglib_syms)
3636

3737
m = np.tile(ss.get_masses_array(), (3,1)).T.ravel()

tests/TestModeSymmetries/test_mode_symmetries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_mode_symmetries(verbose = False):
2727
ss = dyn.structure.generate_supercell(dyn.GetSupercell())
2828

2929
# Load the symmetries from the structure
30-
spglib_sym = spglib.get_symmetry(ss.get_ase_atoms())
30+
spglib_sym = spglib.get_symmetry(ss.get_spglib_cell())
3131
symmetries = CC.symmetries.GetSymmetriesFromSPGLIB(spglib_sym)
3232

3333
# Select only one of the problematic symmetries

tests/TestPhononSupercell/_test_pol_supercell.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
print("Please, install spglib if you want to run the test on the symmetries.")
8080
exit(0)
8181

82-
spglib_sym = spglib.get_symmetry(dyn_realspace.structure.get_ase_atoms())
82+
spglib_sym = spglib.get_symmetry(dyn_realspace.structure.get_spglib_cell())
8383
symmetries = CC.symmetries.GetSymmetriesFromSPGLIB(spglib_sym, False)
8484

8585

tests/TestQStarWithSPGLIB/test_qstar_with_spglib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ def test_qstar_with_spglib():
2020
print("The total number of q:")
2121
print(len(cmca_dyn.q_tot))
2222

23-
print("Space group:", spglib.get_spacegroup(cmca_dyn.structure.get_ase_atoms()))
23+
print("Space group:", spglib.get_spacegroup(cmca_dyn.structure.get_spglib_cell()))
2424
print("Number of symmetries:")
25-
syms = spglib.get_symmetry(cmca_dyn.structure.get_ase_atoms())
25+
syms = spglib.get_symmetry(cmca_dyn.structure.get_spglib_cell())
2626
print(len(syms["rotations"]))
2727

2828
assert len(cmca_dyn.q_stars) == 8

0 commit comments

Comments
 (0)