Coverage for ase/stress.py: 93.18%

44 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-27 15:21 +0000

1import numpy as np 

2 

3# The indices of the full stiffness matrix of (orthorhombic) interest 

4voigt_notation = [(0, 0), (1, 1), (2, 2), (1, 2), (0, 2), (0, 1)] 

5 

6 

7def get_elasticity_tensor(atoms, h=0.001, verbose=False): 

8 """ 

9 

10 1 dE dσ_ij 

11 C = - ----------- = ----- 

12 ijkl V dε_ij dε_kl dε_kl 

13 

14 """ 

15 cell0 = atoms.cell.copy() 

16 C_ijkl = np.zeros((3, 3, 3, 3)) 

17 f = voigt_6_to_full_3x3_stress 

18 for k in range(3): 

19 for l in range(3): 

20 strain = np.eye(3) 

21 strain[k, l] += h 

22 atoms.set_cell(cell0 @ strain, scale_atoms=True) 

23 stress_plus_ij = f(atoms.get_stress()) 

24 strain[k, l] -= 2 * h 

25 atoms.set_cell(cell0 @ strain, scale_atoms=True) 

26 stress_minus_ij = f(atoms.get_stress()) 

27 atoms.set_cell(cell0, scale_atoms=True) 

28 

29 C_ijkl[k, l] = (stress_plus_ij - stress_minus_ij) / (2 * h) 

30 

31 if verbose: 

32 for i in range(3): 

33 for j in range(3): 

34 print(f'C_ijkl[{i}, {j}] =') 

35 for k in range(3): 

36 for l in range(3): 

37 print(round(C_ijkl[i, j, k, l], 2), end=' ') 

38 print() 

39 print() 

40 print() 

41 

42 return C_ijkl 

43 

44 

45def full_3x3_to_voigt_6_index(i, j): 

46 if i == j: 

47 return i 

48 return 6 - i - j 

49 

50 

51def voigt_6_to_full_3x3_strain(strain_vector): 

52 """ 

53 Form a 3x3 strain matrix from a 6 component vector in Voigt notation 

54 """ 

55 e1, e2, e3, e4, e5, e6 = np.transpose(strain_vector) 

56 return np.transpose( 

57 [ 

58 [1.0 + e1, 0.5 * e6, 0.5 * e5], 

59 [0.5 * e6, 1.0 + e2, 0.5 * e4], 

60 [0.5 * e5, 0.5 * e4, 1.0 + e3], 

61 ] 

62 ) 

63 

64 

65def voigt_6_to_full_3x3_stress(stress_vector): 

66 """ 

67 Form a 3x3 stress matrix from a 6 component vector in Voigt notation 

68 """ 

69 s1, s2, s3, s4, s5, s6 = np.transpose(stress_vector) 

70 return np.transpose([[s1, s6, s5], [s6, s2, s4], [s5, s4, s3]]) 

71 

72 

73def full_3x3_to_voigt_6_strain(strain_matrix): 

74 """ 

75 Form a 6 component strain vector in Voigt notation from a 3x3 matrix 

76 """ 

77 strain_matrix = np.asarray(strain_matrix) 

78 return np.transpose( 

79 [ 

80 strain_matrix[..., 0, 0] - 1.0, 

81 strain_matrix[..., 1, 1] - 1.0, 

82 strain_matrix[..., 2, 2] - 1.0, 

83 strain_matrix[..., 1, 2] + strain_matrix[..., 2, 1], 

84 strain_matrix[..., 0, 2] + strain_matrix[..., 2, 0], 

85 strain_matrix[..., 0, 1] + strain_matrix[..., 1, 0], 

86 ] 

87 ) 

88 

89 

90def full_3x3_to_voigt_6_stress(stress_matrix): 

91 """ 

92 Form a 6 component stress vector in Voigt notation from a 3x3 matrix 

93 """ 

94 stress_matrix = np.asarray(stress_matrix) 

95 return np.transpose( 

96 [ 

97 stress_matrix[..., 0, 0], 

98 stress_matrix[..., 1, 1], 

99 stress_matrix[..., 2, 2], 

100 (stress_matrix[..., 1, 2] + stress_matrix[..., 2, 1]) / 2, 

101 (stress_matrix[..., 0, 2] + stress_matrix[..., 2, 0]) / 2, 

102 (stress_matrix[..., 0, 1] + stress_matrix[..., 1, 0]) / 2, 

103 ] 

104 )