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