Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from montoyjh/shyam_master
Browse files Browse the repository at this point in the history
generic rotation method
shyamd committed Apr 7, 2016
2 parents 5dd83a7 + a7580cd commit 02cf026
Showing 4 changed files with 37 additions and 37 deletions.
12 changes: 12 additions & 0 deletions pymatgen/analysis/elasticity/elastic.py
Original file line number Diff line number Diff line change
@@ -156,6 +156,18 @@ def full_tensor(self):
c[j, i, l, k] = c[k, l, i, j] = self[p, q]
return c

def transform(self, symm_op):
"""
Returns a transformed tensor based on input of symmetry operation
Args:
symm_op (symm_op): symmetry operation
"""

new_tensor = symm_op.transform_tensor(self.full_tensor)
return ElasticTensor.from_full_tensor(new_tensor)


def energy_density(self,strain):
"""
Calculates the elastic energy density due to a strain
2 changes: 1 addition & 1 deletion pymatgen/analysis/piezo.py
Original file line number Diff line number Diff line change
@@ -123,7 +123,7 @@ def transform(self, sym):
Args:
symp (SymmOp): symmetry operation
"""
return PiezoTensor.from_full_tensor(sym.transform_r3_tensor(self.full_tensor))
return PiezoTensor.from_full_tensor(sym.transform_tensor(self.full_tensor))

def is_valid(self, structure, symprec=0.1, tol=1e-3):
"""
45 changes: 17 additions & 28 deletions pymatgen/core/operations.py
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@
import numpy as np
import re
from math import sin, cos, pi, sqrt
import string

from monty.json import MSONable

@@ -134,38 +135,26 @@ def apply_rotation_only(self, vector):
"""
return np.dot(self.rotation_matrix, vector)

def transform_r2_tensor(self, tensor):
"""
Applies the rotation portion to a rank 2 tensor.
Args:
tensor (3x3 array): a rank 2 tensor
"""

return np.einsum('ai,bj,ab->ij',self.rotation_matrix,
self.rotation_matrix,tensor)

def transform_r3_tensor(self, tensor):
"""
Applies the rotation portion to a rank 3 tensor.
Args:
tensor (3x3x3 array): a rank 3 tensor
"""
return np.einsum('ai,bj,ck,ijk->abc',self.rotation_matrix,
self.rotation_matrix,self.rotation_matrix,tensor)

def transform_r4_tensor(self, tensor):
def transform_tensor(self, tensor):
"""
Applies the rotation portion to a rank 4 tensor.
Applies rotation portion to a tensor
Args:
tensor (3x3x3x3 array): a rank 4 tensor
"""
return np.einsum('ai,bj,ck,dl,ijkl->abcd',self.rotation_matrix,
self.rotation_matrix,self.rotation_matrix,
self.rotation_matrix,tensor)

tensor (numpy array): a rank n tensor
"""
dim = tensor.shape
t_rank = len(dim)
assert all([i == dim[0] for i in dim])
# Build einstein sum string
lc = string.lowercase
indices = lc[:t_rank], lc[t_rank:2*t_rank]
einsum_string = ','.join([a+i for a,i in zip(*indices)])
einsum_string += ',{}->{}'.format(*indices)
einsum_args = [self.rotation_matrix]*t_rank + [tensor]

return np.einsum(einsum_string, *einsum_args)

def are_symmetrically_related(self, point_a, point_b, tol=0.001):
"""
Checks if two points are symmetrically related.
15 changes: 7 additions & 8 deletions pymatgen/core/tests/test_operations.py
Original file line number Diff line number Diff line change
@@ -60,17 +60,17 @@ def test_apply_rotation_only(self):
self.assertArrayAlmostEqual(
rotate_only + self.op.translation_vector, newcoord, 2)

def test_transform_r2_tensor(self):
def test_transform_tensor(self):
# Rank 2
tensor = np.arange(0, 9).reshape(3, 3)
new_tensor = self.op.transform_r2_tensor(tensor)
new_tensor = self.op.transform_tensor(tensor)
self.assertArrayAlmostEqual(new_tensor,
[[2.7320508, 1.73205079, 4.2320508],
[3.73205078, 1.26794917, 3.330127],
[8.6961524, 3.0621778, 8.]], 5)

def test_transform_r3_tensor(self):
# Rank 3
tensor=np.arange(0, 27).reshape(3, 3, 3)
new_tensor=self.op.transform_r3_tensor(tensor)
new_tensor=self.op.transform_tensor(tensor)
self.assertArrayAlmostEqual(new_tensor,
[[[12.12916506, 4.61602535, 11.92820319],
[7.34807613, 2.33493645, 6.19615237],
@@ -83,10 +83,9 @@ def test_transform_r3_tensor(self):
[[36.32050788, 10.73205068, 28.820508],
[12.73205066, 3.67949186, 9.9185842],
[33.2846096, 9.650635, 26.]]], 5)

def test_transform_r4_tensor(self):
# Rank 4
tensor=np.arange(0, 81).reshape(3, 3, 3, 3)
new_tensor=self.op.transform_r4_tensor(tensor)
new_tensor=self.op.transform_tensor(tensor)
self.assertArrayAlmostEqual(new_tensor,
[[[[50.98076169, 15.5262792, 41.48557134],
[19.25832996, 5.66025391, 15.21410143],

0 comments on commit 02cf026

Please sign in to comment.