Skip to content

Instantly share code, notes, and snippets.

@terencezl
Last active December 9, 2018 02:44
Show Gist options
  • Save terencezl/e6c2b008fd8f2b36840dafa244f78b1d to your computer and use it in GitHub Desktop.
Save terencezl/e6c2b008fd8f2b36840dafa244f78b1d to your computer and use it in GitHub Desktop.
get_symmetrized_elastic_tensor
import warnings
import numpy as np
import pandas as pd
from pymatgen.analysis.elasticity.elastic import ElasticTensor
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
def get_symmetrized_elastic_tensor(C, structure, align=False, tol=1e-4):
"""
Parameters:
C: a 6 x 6 elasic tensor.
structure: the structure used in the calculation, here given to detect symmetry.
align: if the tensor is provided in the standard orientation, but the structure is not,
switch to True to align the structure.
tol: tolerance for pymatgen.analysis.elasticity.elastic.ElasticTensor initializer,
here default to 1e-4 rather than 1e-5.
Return:
C_prime: the symmetrized elastic tensor.
"""
sga = SpacegroupAnalyzer(SpacegroupAnalyzer(structure).find_primitive())
if align:
sga = SpacegroupAnalyzer(sga.get_primitive_standard_structure())
ops_list = sga.get_symmetry_operations(cartesian=True)
ops_rotation_list = [op.rotation_matrix for op in ops_list]
# ops_rotation_unique_list = []
# for i in ops_list:
# should_add = True
# for j in ops_rotation_unique_list:
# if np.allclose(i.rotation_matrix, j):
# should_add = False
# if should_add:
# ops_rotation_unique_list.append(i.rotation_matrix)
with warnings.catch_warnings():
warnings.simplefilter('ignore')
C = ElasticTensor(ElasticTensor(C).symmetrized)
C_prime_sum = 0
for idx, op in enumerate(ops_rotation_list):
C_prime = ElasticTensor.from_full_tensor(np.einsum('im,jn,mnop,ok,pl', op, op, C.full_tensor, op.T, op.T), tol=tol)
C_prime_sum += C_prime
# print(op)
# print(pd.DataFrame(C_prime))
C_prime = C_prime_sum / (idx + 1)
print(sga.get_spacegroup_symbol(), sga.get_point_group(), len(ops_rotation_list))
return C_prime
def get_MP_data(chemsys_formula_id, data_type='vasp', prop=''):
m = mg.MPRester()
df = pd.io.json.json_normalize(m.get_data(chemsys_formula_id, data_type, prop)).set_index('material_id')\
.drop(['spacegroup.source', 'spacegroup.point_group', 'spacegroup.hall'], axis=1)
for col in df.columns:
if 'unit_cell_formula.' in col:
df.drop(col, axis=1, inplace=True)
return df
# test
for i in ['Sc', 'Ti', 'V', 'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn']:
print(i)
for idx, data in get_MP_data(i + '-N').iterrows():
if ('elasticity.elastic_tensor' in data) and (not np.isnan(data['elasticity.elastic_tensor']).all()):
st = mg.Structure.from_str(data['cif'], fmt='cif')
C = pd.DataFrame(data['elasticity.elastic_tensor'], index=range(1, 7), columns=range(1, 7))
C_prime = pd.DataFrame(get_symmetrized_elastic_tensor(C, st, align=True), index=range(1, 7), columns=range(1, 7))
print(C_prime)
print(C_prime - C)
print('=====')
@MilMou
Copy link

MilMou commented Dec 9, 2018

hello
i have run this py, but it seemed not work:

'Traceback (most recent call last):
File "get_symmetrized_elastic_tensor.py", line 62, in
for idx, data in get_MP_data(i + '-N').iterrows():
File "get_symmetrized_elastic_tensor.py", line 51, in get_MP_data
m = mg.MPRester()
NameError: name 'mg' is not defined'

what should i do please?

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment