Upload
suitzero
View
482
Download
1
Embed Size (px)
Citation preview
The gravitational N -body problemwith F#
Contents
explain basic concept of n-body physic example.explain governing equationsvideo watchingvarious way to implement
N-Body simulation
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-linearstructure 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.
Governing equations
assome!
implement with f#
let bodyInteraction posMass0 posMass1 = let r = posMass0 - posMass1 let distSqr = (Vector3.Dot(r,r)) + softneingSquared let invDist = 1.0f / (sqrt distSqr) let invDistCube = invDist * invDist * invDist
r * invDistCube
let computeAffectedGravitation pos = active |> List.map (fun (Body(p,v)) -> bodyInteraction pos p) |> List.reduce (fun acc x -> acc + x)
impelment with f#
let moveBodyAsync f timeDelta (Body (position,velocity)) = async { let force = computeAffectedGravitation position // invMass == 1.0f let newVelocity = velocity + Vector3.Scale(force,timeDelta) // iterate let newVelocity = newVelocity * dampling
let newPosition = position + Vector3.Scale(newVelocity,timeDelta) // We're done! return Body(newPosition,newVelocity) }
let moveBodies t = let active' = active |> List.map (moveBody (theFunction t) timeDelta) active <- active'
wanna parallelism?
use this! async{ } The problems should be solved when you move to parallel from linear codeshared state -> immutablilityinversion of control -> async {...}
upgrade version for Parallellet moveBodyAsync f timeDelta (Body (position,velocity)) = async { let force = computeAffectedGravitation position // invMass == 1.0f let newVelocity = velocity + Vector3.Scale(force,timeDelta) // iterate let newVelocity = newVelocity * dampling let newPosition = position + Vector3.Scale(newVelocity,timeDelta) // We're done! return Body(newPosition,newVelocity) }let moveBodiesAsync t = let active' = active |> List.map (moveBodyAsync (theFunction t) timeDelta) |> Async.Parallel |> Async.RunSynchronously active <- Array.toList(active')ps. they said 'scaling up is also eazy when you using f# agents'
But Still slow
o(n^2)
another aprocche to improve performace
(barnes-hut (divide and conquer(octree)))
particle mesh method? n > 10^5
octree
barnes-hut
barnes-hut
build up octree->reculsively compute the mass and center of mass of each cube by adding up all of the masses contained within that cube->compute the forces on each object by reculsivey traversing the octree
o(n^2) -> o(nlogn)
reference
n-bodyhttp://en.wikipedia.org/wiki/N-body_problemhttp://www.stanford.edu/class/cme212/assignment3.pdf
f#http://www.microsoftpdc.com/2009/FT20
Thaaannnnkkkkks