Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. import bpy
  2. import numpy as np
  3.  
  4.  
  5. def RK4(x, v, n, h, F):
  6.  
  7. for i in range(n): # written for readability, not speed
  8.  
  9. kv1 = F(x[:, i])
  10. kx1 = v[:, i]
  11.  
  12. kv2 = F(x[:, i] + kx1 * (h / 2.0))
  13. kx2 = v[:, i] + kv1 * (h / 2.0)
  14.  
  15. kv3 = F(x[:, i] + kx2 * (h / 2.0))
  16. kx3 = v[:, i] + kv2 * (h / 2.0)
  17.  
  18. kv4 = F(x[:, i] + kx3 * h)
  19. kx4 = v[:, i] + kv3 * h
  20.  
  21. v[:, i + 1] = v[:, i] + (h / 6.0) * (kv1 + 2. * (kv2 + kv3) + kv4)
  22. x[:, i + 1] = x[:, i] + (h / 6.0) * (kx1 + 2. * (kx2 + kx3) + kx4)
  23.  
  24.  
  25. def acc(x):
  26. """ acceleration due to the sun's gravity (NumPy version) """
  27. return -Gm * x / (((x**2).sum(axis=1)**1.5)[:, None])
  28.  
  29.  
  30. def make_from_pydata(named, verts, edges):
  31. profile_mesh = bpy.data.meshes.new(named)
  32. profile_mesh.from_pydata(verts, edges, [])
  33. profile_mesh.update()
  34. profile_object = bpy.data.objects.new(named, profile_mesh)
  35. bpy.context.scene.objects.link(profile_object)
  36.  
  37.  
  38. # m^3 s^-2 (Wikipedia Standard Gravitational Parameter)
  39. Gm = 1.3271244002E+20
  40. t_year = 31558464. # s (roughly)
  41. scale = 4.0E-11 # blender units per meter
  42.  
  43. n_frames = 150
  44. Dt = t_year / float(n_frames) # time step
  45.  
  46. n = 8
  47.  
  48. X = np.zeros((n, 1000, 3))
  49. V = np.zeros((n, 1000, 3))
  50. T = np.zeros((n, 1000))
  51.  
  52. tilt = 30. * (np.pi / 180.) # radians
  53. sin_tilt, cos_tilt = np.sin(tilt), np.cos(tilt)
  54.  
  55. # start in the same place...
  56. X[:, 0] = np.array([1.5E+11, 0.0, 0.0])[None, :]
  57.  
  58. V[:, 0] = 29300. * (np.linspace(0.5, 1.2, n)[:, None] *
  59. np.array([0.0, cos_tilt, sin_tilt])[None, :]) # ...but different initial velocities
  60.  
  61. # NOTE!! This is just for quickie demos only.
  62. # Will give wrong result if step size too big.
  63. RK4(X, V, n_frames, Dt, acc) # pre-calculate orbits
  64.  
  65. p_size, s_size = 0.2, 0.5
  66.  
  67. # Create the Universe
  68. ok = bpy.ops.mesh.primitive_ico_sphere_add(size=s_size, location=(0.0, 0.0, 0.0))
  69. sun = bpy.context.active_object
  70. sun.name = "Sun"
  71.  
  72. n_echos, frames_per_echo = 20, 1
  73. e_sizes = np.linspace(p_size, 0, n_echos + 1)[:-1]
  74.  
  75. paths = {}
  76.  
  77.  
  78. for i in range(n):
  79.  
  80. paths[i] = []
  81. for i_frame in range(n_frames):
  82. location = scale * X[i, i_frame]
  83. paths[i].append(location)
  84. for iecho in range(n_echos):
  85. i_frame_echo = max(0, i_frame - frames_per_echo * (iecho + 1))
  86. location = scale * X[i, i_frame_echo]
  87. paths[i].append(location)
  88.  
  89. for p in sorted(paths.keys()):
  90. verts = paths[p]
  91. edges = [(i, i + 1) for i in range(len(verts) - 1)]
  92. make_from_pydata("p_" + str(p), verts, edges)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement