The gravitational N -body pro

646 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
646
On SlideShare
0
From Embeds
0
Number of Embeds
18
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

The gravitational N -body pro

  1. 1. The gravitational N -body problem with F#
  2. 2. Contents <ul><li>explain basic concept of n-body physic example. </li></ul><ul><li>explain governing equations </li></ul><ul><li>video watching </li></ul><ul><li>various way to implement </li></ul>
  3. 3. N-Body simulation <ul><li>N -body simulation  is a simulation of a  dynamical system  of particles, usually under the influence of physical forces, such as gravity (see  n-body problem ). In cosmology, they are used to study processes of non-linear structure formation  such as the process of forming  galaxy filaments  and  galaxy halos  from  dark matter  in  physical cosmology . Direct  N -body simulations are used to study the dynamical evolution of star clusters. </li></ul>
  4. 4. Governing equations <ul><li>  </li></ul>
  5. 5. assome! <ul><li>  </li></ul>
  6. 6. implement with f# <ul><li>let bodyInteraction posMass0 posMass1 =  </li></ul><ul><li>     let r = posMass0 - posMass1     </li></ul><ul><li>     let distSqr = (Vector3.Dot(r,r)) + softneingSquared </li></ul><ul><li>     let invDist = 1.0f / (sqrt distSqr) </li></ul><ul><li>     let invDistCube = invDist * invDist * invDist </li></ul><ul><li>     r * invDistCube </li></ul><ul><li>let computeAffectedGravitation pos = </li></ul><ul><li>     active |> List.map (fun (Body(p,v)) -> bodyInteraction pos p) |> List.reduce (fun acc x -> acc + x) </li></ul>
  7. 7. impelment with f# <ul><li>let moveBodyAsync f timeDelta (Body (position,velocity)) = </li></ul><ul><li>     async { </li></ul><ul><li>     let force = computeAffectedGravitation position </li></ul><ul><li>     // invMass == 1.0f </li></ul><ul><li>     let newVelocity = velocity + Vector3.Scale(force,timeDelta)  // iterate  </li></ul><ul><li>     let newVelocity = newVelocity * dampling </li></ul><ul><li>     let newPosition = position + Vector3.Scale(newVelocity,timeDelta) </li></ul><ul><li>     // We're done! </li></ul><ul><li>     return Body(newPosition,newVelocity) </li></ul><ul><li>     } </li></ul><ul><li>let moveBodies t =  </li></ul><ul><li>         let active' = active |> List.map (moveBody (theFunction t) timeDelta) </li></ul><ul><li>         active <- active' </li></ul>
  8. 8. wanna parallelism? <ul><li>use this! async{ }  </li></ul><ul><li>The problems should be solved when you move to parallel from linear code </li></ul><ul><li>shared state -> immutablility </li></ul><ul><li>inversion of control -> async {...} </li></ul>
  9. 9. upgrade version for Parallel <ul><li>let moveBodyAsync f timeDelta (Body (position,velocity)) = </li></ul><ul><li>     async { </li></ul><ul><li>     let force = computeAffectedGravitation position </li></ul><ul><li>     // invMass == 1.0f </li></ul><ul><li>     let newVelocity = velocity + Vector3.Scale(force,timeDelta)  // iterate  </li></ul><ul><li>     let newVelocity = newVelocity * dampling </li></ul><ul><li>     let newPosition = position + Vector3.Scale(newVelocity,timeDelta) </li></ul><ul><li>     // We're done! </li></ul><ul><li>     return Body(newPosition,newVelocity) </li></ul><ul><li>     } </li></ul><ul><li>let moveBodiesAsync t =  </li></ul><ul><li>         let active' = active |> List.map (moveBodyAsync (theFunction t) timeDelta) |> Async.Parallel |> Async.RunSynchronously          </li></ul><ul><li>         active <- Array.toList(active') </li></ul><ul><li>ps. they said 'scaling up is also eazy when you using f# agents' </li></ul>
  10. 10. But Still slow <ul><li>o(n^2) </li></ul><ul><li>another aprocche to improve performace </li></ul><ul><li>(barnes-hut (divide and conquer(octree)))  </li></ul><ul><li>particle mesh method? n > 10^5 </li></ul>
  11. 11. octree <ul><li>  </li></ul>
  12. 12. barnes-hut <ul><li>  </li></ul>
  13. 13. barnes-hut <ul><li>build up octree </li></ul><ul><li>-> </li></ul><ul><li>reculsively compute the mass and center of mass of each cube by adding up all of the masses contained within that cube </li></ul><ul><li>-> </li></ul><ul><li>compute the forces on each object by reculsivey traversing the octree </li></ul><ul><li>o(n^2) -> o(nlogn) </li></ul>
  14. 14. reference <ul><li>n-body </li></ul><ul><li>http://en.wikipedia.org/wiki/N-body_problem </li></ul><ul><li>http://www.stanford.edu/class/cme212/assignment3.pdf </li></ul><ul><li>f# </li></ul><ul><li>http://www.microsoftpdc.com/2009/FT20 </li></ul>
  15. 15. Thaaannnnkkkkks  

×