acceleration.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. """Defines the possible routines for computing the gravitational forces in the
  2. simulation.
  3. All the methods in this file require a position (n, 3) vector, a mass (n, )
  4. vector and an optional softening scale float."""
  5. import ctypes
  6. import numpy.ctypeslib as ctl
  7. import numpy as np
  8. from numba import jit
  9. def bruteForce(r_vec, mass, soft=0.):
  10. """Calculates the acceleration generated by a set of masses on themselves.
  11. Complexity O(n*m) where n is the total number of masses and m is the
  12. number of massive particles.
  13. Parameters:
  14. r_vec (array): list of particles positions.
  15. Shape (n, 3) where n is the number of particles
  16. mass (array): list of particles masses.
  17. Shape (n,)
  18. soft (float): characteristic plummer softening length scale
  19. Returns:
  20. forces (array): list of forces acting on each particle.
  21. Shape (n, 3)
  22. """
  23. # Only calculate forces from massive particles
  24. mask = mass!=0
  25. massMassive = mass[mask]
  26. rMassive_vec = r_vec[mask]
  27. # x m x 1 matrix (m = number of massive particles) for broadcasting
  28. mass_mat = massMassive.reshape(1, -1, 1)
  29. # Calculate displacements
  30. # r_ten is the direction of the pairwise displacements. Shape (n, m, 3)
  31. # r_mat is the absolute distance of the pairwise displacements. (n, m, 1)
  32. r_ten = rMassive_vec.reshape(1, -1, 3) - r_vec.reshape(-1, 1, 3)
  33. r_mat = np.linalg.norm(r_ten, axis=-1, keepdims=True)
  34. # Avoid division by zeros
  35. # $a = M / (r + \epsilon)^2$, where $\epsilon$ is the softening scale
  36. # r_ten/r_mat gives the direction unit vector
  37. accel = np.divide(r_ten * mass_mat/(r_mat+soft)**2, r_mat,
  38. where=r_ten.astype(bool), out=r_ten) # Reuse memory from r_ten
  39. return accel.sum(axis=1) # Add all forces on each particle
  40. @jit(nopython=True) # Numba annotation
  41. def bruteForceNumba(r_vec, mass, soft=0.):
  42. """Calculates the acceleration generated by a set of masses on themselves.
  43. It is done in the same way as in bruteForce, but this
  44. method is ran through Numba"""
  45. mask = mass!=0
  46. massMassive = mass[mask]
  47. rMassive_vec = r_vec[mask]
  48. mass_mat = massMassive.reshape(1, -1, 1)
  49. r_ten = rMassive_vec.reshape(1, -1, 3) - r_vec.reshape(-1, 1, 3)
  50. # Avoid np.linalg.norm to allow Numba optimizations
  51. r_mat = np.sqrt(r_ten[:,:,0:1]**2 + r_ten[:,:,1:2]**2 + r_ten[:,:,2:3]**2)
  52. r_mat = np.where(r_mat == 0, np.ones_like(r_mat), r_mat)
  53. accel = r_ten/r_mat * mass_mat/(r_mat+soft)**2
  54. return accel.sum(axis=1) # Add all forces in each particle
  55. @jit(nopython=True) # Numba annotation
  56. def bruteForceNumbaOptimized(r_vec, mass, soft=0.):
  57. """Calculates the acceleration generated by a set of masses on themselves.
  58. This is optimized for high performance with Numba. All massive particles
  59. must appear first."""
  60. accel = np.zeros_like(r_vec)
  61. # Use superposition to add all the contributions
  62. n = r_vec.shape[0] # Number of particles
  63. delta = np.zeros((3,)) # Only allocate this once
  64. for i in range(n):
  65. # Only consider pairs with at least one massive particle i
  66. if mass[i] == 0: break
  67. for j in range(i+1, n):
  68. # Explicitely separate components for high performance
  69. # i.e. do not do delta = r_vec[j] - r_vec[i]
  70. # (The effect of this is VERY relevant (x10) and has to do with
  71. # memory reallocation) Numba will vectorize the loops.
  72. for k in range(3): delta[k] = r_vec[j,k] - r_vec[i,k]
  73. r = np.sqrt(delta[0]*delta[0] + delta[1]*delta[1] + delta[2]*delta[2])
  74. tripler = (r+soft)**2 * r
  75. # Compute acceleration on first particle
  76. mr3inv = mass[i]/(tripler)
  77. # Again, do NOT do accel[j] -= mr3inv * delta
  78. for k in range(3): accel[j,k] -= mr3inv * delta[k]
  79. # Compute acceleration on second particle
  80. # For pairs with one massless particle, no reaction force
  81. if mass[j] == 0: break
  82. # Otherwise, opposite direction (+)
  83. mr3inv = mass[j]/(tripler)
  84. for k in range(3): accel[i,k] += mr3inv * delta[k]
  85. return accel
  86. # C++ interface, load library
  87. ACCLIB = None
  88. def loadCPPLib():
  89. """Loads the C++ shared library to the global variable ACCLIB. Must be
  90. called before using the library."""
  91. global ACCLIB
  92. ACCLIB = ctypes.CDLL('cpp/acclib.so')
  93. # Define appropiate types for library functions
  94. doublepp = np.ctypeslib.ndpointer(dtype=np.uintp) # double**
  95. doublep = ctl.ndpointer(np.float64, flags='aligned, c_contiguous')#double*
  96. # Check cpp/acclib.cpp for function signatures
  97. ACCLIB.bruteForceCPP.argtypes = [doublepp, doublep,
  98. ctypes.c_int, ctypes.c_double]
  99. ACCLIB.barnesHutCPP.argtypes = [doublepp, doublep,
  100. ctypes.c_int, ctypes.c_double, ctypes.c_double,
  101. ctypes.c_double, ctypes.c_double, ctypes.c_double]
  102. def bruteForceCPP(r_vec, m_vec, soft=0.):
  103. """Calculates the acceleration generated by a set of masses on themselves.
  104. This is ran in a shared C++ library through Brute Force (pairwise sums)
  105. Massive particles must appear first."""
  106. # Convert array to data required by C++ library
  107. if ACCLIB is None: loadCPPLib() # Singleton pattern
  108. # Change type to be appropiate for calling library
  109. r_vec_c = (r_vec.ctypes.data + np.arange(r_vec.shape[0])
  110. * r_vec.strides[0]).astype(np.uintp)
  111. # Set return type as double*
  112. ACCLIB.bruteForceCPP.restype = np.ctypeslib.ndpointer(dtype=np.float64,
  113. shape=(r_vec.shape[0]*3,))
  114. # Call the C++ function: double* bruteForceCPP
  115. accel = ACCLIB.bruteForceCPP(r_vec_c, m_vec, r_vec.shape[0], soft)
  116. # Change shape to get the expected Numpy array (n, 3)
  117. accel.shape = (-1, 3)
  118. return accel
  119. def barnesHutCPP(r_vec, m_vec, soft=0.):
  120. """Calculates the acceleration generated by a set of masses on themselves.
  121. This is ran in a shared C++ library using a BarnesHut tree"""
  122. # Convert array to data required by C++ library
  123. if ACCLIB is None: loadCPPLib() # Singleton pattern
  124. # Change type to be appropiate for calling library
  125. r_vec_c = (r_vec.ctypes.data + np.arange(r_vec.shape[0])
  126. * r_vec.strides[0]).astype(np.uintp)
  127. # Set return type as double*
  128. ACCLIB.barnesHutCPP.restype = np.ctypeslib.ndpointer(dtype=np.float64,
  129. shape=(r_vec.shape[0]*3,))
  130. # Explicitely pass the corner and size of the box for the top node
  131. px, py, pz = np.min(r_vec, axis=0)
  132. size = np.max(np.max(r_vec, axis=0) - np.min(r_vec, axis=0))
  133. # Call the C++ function: double* barnesHutCPP
  134. accel = ACCLIB.barnesHutCPP(r_vec_c, m_vec, r_vec.shape[0],
  135. size, px, py, pz, soft)
  136. # Change shape to get the expected Numpy array (n, 3)
  137. accel.shape = (-1, 3)
  138. return accel