Guest User

Untitled

a guest
Dec 6th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. #!/usr/bin/env python2
  2.  
  3. #
  4. # chaos - jzhu98
  5. # See bottom for license.
  6. #
  7.  
  8. from __future__ import division
  9.  
  10. from enum import Enum
  11. from copy import deepcopy
  12. from random import random as r
  13. from visual import *
  14. from octree import Octree
  15.  
  16.  
  17. class Particle(sphere):
  18. def __init__(self, pos, vel, col, rad, mass):
  19. super(self.__class__, self).__init__(
  20. pos=pos,
  21. radius=rad,
  22. color=col
  23. )
  24. self.vel = vel
  25. self.mass = mass
  26.  
  27.  
  28. class Dust(sphere):
  29. def __init__(self, pos, vel, col, rad, mass):
  30. super(self.__class__, self).__init__(
  31. pos=pos,
  32. radius=rad,
  33. color=col,
  34. make_trail=True
  35. )
  36. self.vel = vel
  37. self.mass = mass
  38.  
  39.  
  40. def make_molecules(n_molecules, radius, mass):
  41. molecules = [Dust(
  42. pos=vector(0, 0, 0),
  43. vel=vector(0, 0, 0),
  44. col=color.red,
  45. rad=0.05,
  46. mass=1
  47. )]
  48. for i in xrange(n_molecules):
  49. molecules.append(Particle(
  50. pos=vector(r() - 0.5, r() - 0.5, r() - 0.5),
  51. vel=vector(r() - 0.5, r() - 0.5, r() - 0.5),
  52. col=color.blue,
  53. rad=radius,
  54. mass=mass
  55. ))
  56. return molecules
  57.  
  58.  
  59. # Stable (no oscillating velocity) because return to inside bounding box
  60. # is guaranteed
  61. def calc_wall_collision(particle):
  62. box_bounds = 0.5
  63. if abs(particle.pos.x) >= box_bounds:
  64. particle.vel.x *= -1
  65. if abs(particle.pos.y) >= box_bounds:
  66. particle.vel.y *= -1
  67. if abs(particle.pos.z) >= box_bounds:
  68. particle.vel.z *= -1
  69.  
  70.  
  71. def calc_part_collision(p, molecules):
  72. for m in molecules:
  73. # Avoid collision with itself
  74. if p is m:
  75. continue
  76.  
  77. # If collision detected, perform elastic momentum transfer
  78. # (Python simultaneous assignment ftw!)
  79. if mag(p.pos - m.pos) <= p.radius + m.radius:
  80. # If masses are equal, velocities are swapped
  81. if abs(p.mass - m.mass) < 1e-3:
  82. p.vel, m.vel = m.vel, p.vel
  83. else:
  84. p.vel, m.vel = \
  85. (p.vel * (p.mass - m.mass) + 2 * m.mass * m.vel) / (p.mass + m.mass), \
  86. (m.vel * (m.mass - p.mass) + 2 * p.mass * p.vel) / (p.mass + m.mass)
  87. # Process only one collision per frame
  88. break
  89.  
  90.  
  91. def update_velocity(molecules, dt):
  92. for m in molecules:
  93. calc_wall_collision(m)
  94.  
  95. calc_part_collision(molecules[0], molecules)
  96.  
  97.  
  98. def update_position(molecules, dt):
  99. for m in molecules:
  100. m.pos += m.vel * dt
  101.  
  102.  
  103. def main():
  104. container = box(pos=vector(0, 0, 0),
  105. size=vector(1, 1, 1),
  106. color=color.blue,
  107. opacity=0.1)
  108.  
  109. # Generate list of 1 dust particle and 100 random small particles
  110. molecules = make_molecules(n_molecules=250, radius=0.01, mass=0.01)
  111.  
  112. dt = 0.05
  113.  
  114. while 1:
  115. rate(60)
  116. update_velocity(molecules, dt)
  117. update_position(molecules, dt)
  118.  
  119. if __name__ == '__main__':
  120. main()
  121.  
  122. # The MIT License (MIT)
  123. # Copyright (c) 2014 James Zhu
  124. #
  125. # Permission is hereby granted, free of charge, to any person obtaining a copy
  126. # of this software and associated documentation files (the "Software"), to deal
  127. # in the Software without restriction, including without limitation the rights
  128. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  129. # copies of the Software, and to permit persons to whom the Software is
  130. # furnished to do so, subject to the following conditions:
  131. #
  132. # The above copyright notice and this permission notice shall be included in all
  133. # copies or substantial portions of the Software.
  134. #
  135. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  136. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  137. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  138. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  139. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  140. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  141. # SOFTWARE.
Add Comment
Please, Sign In to add comment