15
The gravitational N -body problem with F#

The gravitational N -body pro

Embed Size (px)

Citation preview

Page 1: The gravitational N -body pro

The gravitational N -body problemwith F#

Page 2: The gravitational N -body pro

Contents

explain basic concept of n-body physic example.explain governing equationsvideo watchingvarious way to implement

Page 3: The gravitational N -body pro

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.

Page 4: The gravitational N -body pro

Governing equations

 

Page 5: The gravitational N -body pro

assome!

 

Page 6: The gravitational N -body pro

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)

Page 7: The gravitational N -body pro

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'

Page 8: The gravitational N -body pro

wanna parallelism?

use this! async{ } The problems should be solved when you move to parallel from linear codeshared state -> immutablilityinversion of control -> async {...}

Page 9: The gravitational N -body pro

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'

Page 10: The gravitational N -body pro

But Still slow

o(n^2)

another aprocche to improve performace

(barnes-hut (divide and conquer(octree))) 

particle mesh method? n > 10^5

Page 11: The gravitational N -body pro

octree

 

Page 12: The gravitational N -body pro

barnes-hut

 

Page 13: The gravitational N -body pro

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)

Page 14: The gravitational N -body pro

reference

n-bodyhttp://en.wikipedia.org/wiki/N-body_problemhttp://www.stanford.edu/class/cme212/assignment3.pdf

f#http://www.microsoftpdc.com/2009/FT20

Page 15: The gravitational N -body pro

Thaaannnnkkkkks