oILAB
Loading...
Searching...
No Matches
example.py
Go to the documentation of this file.
1import pyoilab as gb
2import numpy as np
3from scipy.spatial.transform import Rotation
4
5# construct lattice
6A= np.array([[0.5,0.5,0.0],
7 [0.5,0.0,0.5],
8 [0.0,0.5,0.5]])
9lattice=gb.Lattice3D(A)
10
11# construct a lattice vector from its cartesian coordinates
12lv1 = lattice.latticeVector(np.array([1.0,2.0,2.0]))
13
14# print its integer and cartesian coordinates
15print("integer coordinates of lv1 = ", lv1.integerCoordinates())
16cartesianCoordinates= lv1.cartesian()
17print("cartesian coordinates of lv1 = ", cartesianCoordinates)
18
19# reconstruct the lattice vector using the above cartesian coordinates
20lv2 = gb.LatticeVector3D(lv1.cartesian(),lattice)
21
22# confirm that lv1 and lv2 are the same
23#$print("integer coorindates of lv2 = ", lv2.integerCoordinates())
24print("Are lv1 and lv2 identical: ", lv1.integerCoordinates()==lv2.integerCoordinates())
25
26# now change lv2's integer coordinates
27lv2.integerCoordinates(np.array([1,2,3],dtype=np.int64))
28print("Integer coordinates of lv2 changed to = ", lv2.integerCoordinates())
29print("Cartesian coordinates of lv2 = ", lv2.cartesian())
30
31# Demonstrate addition and multiplication
32lv3=4*(lv1+lv2)
33print("Integer coordinates of lv3 = ", lv3.integerCoordinates())
34print("Cartesian coordinates of lv3 = ", lv3.cartesian())
35
36
37# Construct reciprocal lattice vectors
38rlv1= gb.ReciprocalLatticeVector3D(np.array([1,5,6],dtype=np.int64),lattice)
39rlv2= gb.ReciprocalLatticeVector3D(np.array([2.,2.,4.],dtype=np.float64),lattice)
40
41# Demonstrate cross product
42ld=rlv1.cross(rlv2)
43print("Integer coorindates of the resulting lattice direction = ", ld.integerCoordinates())
44print("Is ld perpendicular to rlv1 and rlv2?", (ld.dot(rlv1)==0 and ld.dot(rlv2)==0))
45
46# Demonstrate the lattice box function
47bv1= gb.LatticeVector3D(np.array([1,-1,0]),lattice)
48bv2= gb.LatticeVector3D(np.array([1,1,-2]),lattice)
49bv3= gb.LatticeVector3D(np.array([1,1,1]),lattice)
50bv1= 3*bv1
51bv2= 3*bv2
52bv3= 3*bv3
53config= lattice.box([bv1,bv2,bv3],"config.txt")
54
55# Construct bicrystals with tilt axis
56misorientationAxis = lattice.reciprocalLatticeDirection(np.array([1.0,1.0,1.0]))
57print("Rotation axis = ", misorientationAxis.cartesian())
58rotations= lattice.generateCoincidentLattices(misorientationAxis)
59
60for i, rotation in enumerate(rotations):
61 rot = Rotation.from_matrix(rotation)
62 angle = rot.magnitude() / 2
63 axis = rot.as_rotvec()
64 if np.allclose(axis, 0):
65 angle=0.0
66 axis=np.array([1,0,0])
67
68 # Normalize axis and scale angle
69 half_rotvec = axis / np.linalg.norm(axis) * angle
70 positiveR = Rotation.from_rotvec(half_rotvec).as_matrix()
71 negativeR = Rotation.from_rotvec(-half_rotvec).as_matrix()
72
73 # From the two lattices by rotating the global lattice by +\theta/2 and -\theta/2
74 lattice1 = gb.Lattice3D(A,positiveR)
75 lattice2 = gb.Lattice3D(A,negativeR)
76
77 try:
78 bicrystal = gb.BiCrystal3D(lattice1,lattice2,False)
79 print(f"Matrix {i}: angle - {np.degrees(2*angle):.2f} degrees, sigma = {bicrystal.sigma}")
80 if (bicrystal.sigma<100):
81 csl = bicrystal.csl
82 # box vector along the axis
83 bcslv1 = csl.latticeDirection(misorientationAxis.cartesian()).latticeVector()
84 # box vector orthogonal to bcslv1
85 #bcslv2 = csl.latticeDirection(np.array([1.0,0.0,-1.0])).latticeVector()
86 bcslv2 = csl.latticeDirection(bcslv1.cross().cartesian()).latticeVector()
87 # box vector orthogonal to bcslv1 and bcslv2
88 bcslv3 = csl.latticeDirection(bcslv1.cross(bcslv2).cartesian()).latticeVector()
89 config = bicrystal.box([bcslv1,bcslv2,bcslv3],1,1,"bc"+str(i))
90 except Exception as e:
91 print(f"Caught an exception: {e}")
92
93