# vpython_random_points_in_cylinder_moment_of_inertia

Aug 30th, 2021 (edited)
1,937
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #31_08_21
2.
3. '''
4.
5. CALCULATE THE MOMENT(S) OF INERTIA OF A CYLINDER ROTATING ABOUT CENTER
6.
7.                        |
8.               +--------|--------+
9.               |        |        |
10.               +--------|--------+
11.                        |
12.
13. Ideas from:
14. Rhett Allain's Physics Explained Channel
15. "Finding the moment of inertia for a sphere by making 1000 random points"
16. "Building a circle with random points - cartesian vs. polar coordinates"
17. "Two Ways to Find the Moment of Inertia Tensor for a Solid Cube"
18.
19. tested on: Linux Mint 20.2, Geany 1.36, python 3.8.10,
20. Web Epiphany, i5-7260U CPU @ 2.20GHz × 2, Iris Plus Graphics 640
21. vpython version: ['7.6.1', 'jupyter']
22.
23. In [1]: %timeit 2.0/3.0
24. 7.39 ns ± 0.0186 ns per loop
25.
26. '''
27.
28. from vpython import *
29. import sys
30.
31. def main():
32.     #print(version, '\n')
33.
34.     scene.width, scene.height = 1900, 980
35.     scene.background = color.black
36.     scene.align = 'left'
37.     scene.autoscale = False
38.     scene.autozoom = False
39.     scene.userspin = True
40.     scene.userzoom = True
41.     scene.userpan = False
42.     scene.resizable = False
43.     scene.range = 8.0
44.
45.     '''
46.    For R=10:
47.
48.    (x,0,0) -> 100/12 = 8.3333 # VERY THIN ROD ABOUT ANY AXIS
49.    (x,0,z) -> 103/12 = 8.5833 # ROD ABOUT Y-AXIS
50.    (x,y,z) -> 106/12 = 8.8333 # ROD ABOUT ANY AXIS
51.
52.    '''
53.
54.     SUM = 'xz' # 'x', 'xy', 'xz', 'xyz'
55.
56.     print('\n', 'SUMMATION STRING: ', "'", SUM, "'\n", sep='')
57.
58.     DRAW_CYLINDER = 0
59.
60.     if DRAW_CYLINDER:
62.
63.     N = 5000 if DRAW_CYLINDER else 10000000
64.
65.     LENGTH = 10.0
66.     LENGTH_2 = LENGTH / 2.0
67.     R = 1.0  # RADIUS
68.     M = 1.0  # TOTAL MASS
69.     m = M/N  # PART MASS
70.
71.     # MOMENT OF INERTIA
72.     I = 0.0
73.
74.     # CENTER OF MASS
75.     CM = vec(0.,0.,0.)
76.
77.     for _ in range(N):
78.
79.         while 1:
80.             r = vec(0.0, 2.0*random()-1.0, 2.0*random()-1.0)
81.             if r.mag < R: break
82.         r.x = random() * LENGTH - LENGTH_2
83.
84.         if DRAW_CYLINDER:
86.
87.         if SUM == 'x':              # R²/12
88.             # ∑ mx²
89.             r = vec(r.x, 0., 0.)
90.
91.         elif SUM == 'xz':           # (R²+3)/12
92.             # ∑ m(x² + z²)
93.             r = vec(r.x, 0., r.z)
94.
95.         elif SUM == 'xy':           # (R²+3)/12
96.             # ∑ m(x² + y²)
97.             r = vec(r.x, r.y, 0.)
98.
99.         elif SUM == 'xyz':          # (R²+6)/12
100.             # ∑ m(x² + y² + z²)
101.             r = vec(r.x, r.y, r.z)
102.
103.         else:
104.             print('INVALID SUMMATION STRING')
105.             print("valid strings are: 'x', 'xy', 'xz', 'xyz'")
106.             sys.exit()
107.
108.         I += m * r.mag2
109.
110.         CM += m * r
111.
112.     CM /= M
113.
114.     print('I = ', I, '\n', sep='')
115.     print('ML^2/12 = ', M * LENGTH**2 / 12.0, '\n')
116.
117.     print('CM = ', CM)
118.
119.
120. class g: ...
121.
122.
123. if __name__ == '__main__':
124.     sys.exit(main())
125.
126.
127.
128.
RAW Paste Data