13
13
# as the particle with higher velocity. For simplicity, in this demo we
14
14
# do not apply forces.
15
15
16
- import numpy as np
17
- from fury import window , actor , ui , utils
18
16
import itertools
19
17
18
+ import numpy as np
19
+
20
+ from fury import actor , ui , utils , window
20
21
21
22
##############################################################################
22
23
# Here, we define the edges of the box.
23
24
24
25
25
26
def box_edges (box_lx , box_ly , box_lz ):
26
27
27
- edge1 = 0.5 * np .array ([[ box_lx , box_ly , box_lz ],
28
- [ box_lx , box_ly , - box_lz ],
29
- [ - box_lx , box_ly , - box_lz ],
30
- [ - box_lx , box_ly , box_lz ],
31
- [ box_lx , box_ly , box_lz ]])
32
- edge2 = 0.5 * np . array ([[ box_lx , box_ly , box_lz ],
33
- [box_lx , - box_ly , box_lz ]])
34
- edge3 = 0.5 * np . array ([[ box_lx , box_ly , - box_lz ],
35
- [ box_lx , - box_ly , - box_lz ]] )
36
- edge4 = 0.5 * np .array ([[- box_lx , box_ly , - box_lz ],
37
- [ - box_lx , - box_ly , - box_lz ]])
38
- edge5 = 0.5 * np .array ([[- box_lx , box_ly , box_lz ],
39
- [- box_lx , - box_ly , box_lz ]])
28
+ edge1 = 0.5 * np .array (
29
+ [
30
+ [ box_lx , box_ly , box_lz ],
31
+ [ box_lx , box_ly , - box_lz ],
32
+ [ - box_lx , box_ly , - box_lz ],
33
+ [ - box_lx , box_ly , box_lz ],
34
+ [box_lx , box_ly , box_lz ],
35
+ ]
36
+ )
37
+ edge2 = 0.5 * np .array ([[box_lx , box_ly , box_lz ], [ box_lx , - box_ly , box_lz ]])
38
+ edge3 = 0.5 * np . array ([[ box_lx , box_ly , - box_lz ], [ box_lx , - box_ly , - box_lz ]])
39
+ edge4 = 0.5 * np .array ([[- box_lx , box_ly , - box_lz ], [ - box_lx , - box_ly , - box_lz ]])
40
+ edge5 = 0.5 * np . array ([[ - box_lx , box_ly , box_lz ], [- box_lx , - box_ly , box_lz ]])
40
41
lines = [edge1 , - edge1 , edge2 , edge3 , edge4 , edge5 ]
41
42
return lines
42
43
@@ -46,41 +47,55 @@ def box_edges(box_lx, box_ly, box_lz):
46
47
# When collision happens, the particle with lower velocity gets the
47
48
# color of the particle with higher velocity
48
49
50
+
49
51
def collision ():
50
52
global xyz
51
53
num_vertices = vertices .shape [0 ]
52
54
sec = int (num_vertices / num_particles )
53
55
54
56
for i , j in np .ndindex (num_particles , num_particles ):
55
57
56
- if ( i == j ) :
58
+ if i == j :
57
59
continue
58
60
distance = np .linalg .norm (xyz [i ] - xyz [j ])
59
61
vel_mag_i = np .linalg .norm (vel [i ])
60
62
vel_mag_j = np .linalg .norm (vel [j ])
61
63
# Collision happens if the distance between the centers of two
62
64
# particles is less or equal to the sum of their radii
63
- if ( distance <= (radii [i ] + radii [j ]) ):
65
+ if distance <= (radii [i ] + radii [j ]):
64
66
vel [i ] = - vel [i ]
65
67
vel [j ] = - vel [j ]
66
68
if vel_mag_j > vel_mag_i :
67
- vcolors [i * sec : i * sec + sec ] = \
68
- vcolors [j * sec : j * sec + sec ]
69
+ vcolors [i * sec : i * sec + sec ] = vcolors [j * sec : j * sec + sec ]
69
70
if vel_mag_i > vel_mag_j :
70
- vcolors [j * sec : j * sec + sec ] = \
71
- vcolors [i * sec : i * sec + sec ]
71
+ vcolors [j * sec : j * sec + sec ] = vcolors [i * sec : i * sec + sec ]
72
72
xyz [i ] = xyz [i ] + vel [i ] * dt
73
73
xyz [j ] = xyz [j ] + vel [j ] * dt
74
74
# Collision between particles-walls;
75
- vel [:, 0 ] = np .where (((xyz [:, 0 ] <= - 0.5 * box_lx + radii [:]) |
76
- (xyz [:, 0 ] >= (0.5 * box_lx - radii [:]))),
77
- - vel [:, 0 ], vel [:, 0 ])
78
- vel [:, 1 ] = np .where (((xyz [:, 1 ] <= - 0.5 * box_ly + radii [:]) |
79
- (xyz [:, 1 ] >= (0.5 * box_ly - radii [:]))),
80
- - vel [:, 1 ], vel [:, 1 ])
81
- vel [:, 2 ] = np .where (((xyz [:, 2 ] <= - 0.5 * box_lz + radii [:]) |
82
- (xyz [:, 2 ] >= (0.5 * box_lz - radii [:]))),
83
- - vel [:, 2 ], vel [:, 2 ])
75
+ vel [:, 0 ] = np .where (
76
+ (
77
+ (xyz [:, 0 ] <= - 0.5 * box_lx + radii [:])
78
+ | (xyz [:, 0 ] >= (0.5 * box_lx - radii [:]))
79
+ ),
80
+ - vel [:, 0 ],
81
+ vel [:, 0 ],
82
+ )
83
+ vel [:, 1 ] = np .where (
84
+ (
85
+ (xyz [:, 1 ] <= - 0.5 * box_ly + radii [:])
86
+ | (xyz [:, 1 ] >= (0.5 * box_ly - radii [:]))
87
+ ),
88
+ - vel [:, 1 ],
89
+ vel [:, 1 ],
90
+ )
91
+ vel [:, 2 ] = np .where (
92
+ (
93
+ (xyz [:, 2 ] <= - 0.5 * box_lz + radii [:])
94
+ | (xyz [:, 2 ] >= (0.5 * box_lz - radii [:]))
95
+ ),
96
+ - vel [:, 2 ],
97
+ vel [:, 2 ],
98
+ )
84
99
85
100
86
101
##############################################################################
@@ -94,8 +109,9 @@ def collision():
94
109
box_lz = 10
95
110
steps = 1000
96
111
dt = 0.05
97
- xyz = np .array ([box_lx , box_ly , box_lz ]) * (np .random .rand (num_particles , 3 )
98
- - 0.5 ) * 0.6
112
+ xyz = (
113
+ np .array ([box_lx , box_ly , box_lz ]) * (np .random .rand (num_particles , 3 ) - 0.5 ) * 0.6
114
+ )
99
115
vel = 4 * (np .random .rand (num_particles , 3 ) - 0.5 )
100
116
colors = np .random .rand (num_particles , 3 )
101
117
radii = np .random .rand (num_particles ) + 0.01
@@ -108,22 +124,21 @@ def collision():
108
124
box_centers = np .array ([[0 , 0 , 0 ]])
109
125
box_directions = np .array ([[0 , 1 , 0 ]])
110
126
box_colors = np .array ([[1 , 1 , 1 , 0.2 ]])
111
- box_actor = actor .box (box_centers , box_directions , box_colors ,
112
- scales = (box_lx , box_ly , box_lz ))
127
+ box_actor = actor .box (
128
+ box_centers , box_directions , box_colors , scales = (box_lx , box_ly , box_lz )
129
+ )
113
130
scene .add (box_actor )
114
131
115
132
lines = box_edges (box_lx , box_ly , box_lz )
116
133
line_actor = actor .streamtube (lines , colors = (1 , 0.5 , 0 ), linewidth = 0.1 )
117
134
scene .add (line_actor )
118
135
119
- sphere_actor = actor .sphere (centers = xyz ,
120
- colors = colors ,
121
- radii = radii )
136
+ sphere_actor = actor .sphere (centers = xyz , colors = colors , radii = radii )
122
137
scene .add (sphere_actor )
123
138
124
- showm = window .ShowManager (scene ,
125
- size = (900 , 768 ), reset_camera = True ,
126
- order_transparent = True )
139
+ showm = window .ShowManager (
140
+ scene , size = (900 , 768 ), reset_camera = True , order_transparent = True
141
+ )
127
142
128
143
tb = ui .TextBlock2D (bold = True )
129
144
scene .zoom (0.8 )
@@ -134,9 +149,8 @@ def collision():
134
149
135
150
vertices = utils .vertices_from_actor (sphere_actor )
136
151
vcolors = utils .colors_from_actor (sphere_actor , 'colors' )
137
- no_vertices_per_sphere = len (vertices )/ num_particles
138
- initial_vertices = vertices .copy () - \
139
- np .repeat (xyz , no_vertices_per_sphere , axis = 0 )
152
+ no_vertices_per_sphere = len (vertices ) / num_particles
153
+ initial_vertices = vertices .copy () - np .repeat (xyz , no_vertices_per_sphere , axis = 0 )
140
154
141
155
142
156
def timer_callback (_obj , _event ):
@@ -146,8 +160,7 @@ def timer_callback(_obj, _event):
146
160
xyz = xyz + vel * dt
147
161
collision ()
148
162
149
- vertices [:] = initial_vertices + \
150
- np .repeat (xyz , no_vertices_per_sphere , axis = 0 )
163
+ vertices [:] = initial_vertices + np .repeat (xyz , no_vertices_per_sphere , axis = 0 )
151
164
utils .update_actor (sphere_actor )
152
165
153
166
scene .reset_clipping_range ()
@@ -164,4 +177,4 @@ def timer_callback(_obj, _event):
164
177
if interactive :
165
178
showm .start ()
166
179
167
- window .record (showm .scene , size = (900 , 768 ), out_path = " simple_collisions.png" )
180
+ window .record (showm .scene , size = (900 , 768 ), out_path = ' simple_collisions.png' )
0 commit comments