<s> '''<STR_LIT>''' <EOL> from time import time <EOL> def main ( ) : <EOL> if PYTHON == '<STR_LIT>' : <EOL> pythonjs . configure ( direct_operator = '<STR_LIT:+>' ) <EOL> pythonjs . configure ( direct_operator = '<STR_LIT:*>' ) <EOL> pythonjs . configure ( direct_keys = True ) <EOL> pass <EOL> times = test_nbody ( <NUM_LIT:3> ) <EOL> avg = sum ( times ) / <NUM_LIT> <EOL> print ( avg ) <EOL> def combinations ( l ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> for x in range ( len ( l ) - <NUM_LIT:1> ) : <EOL> ls = l [ x + <NUM_LIT:1> : ] <EOL> for y in ls : <EOL> result . append ( ( l [ x ] , y ) ) <EOL> return result <EOL> PI = <NUM_LIT> <EOL> SOLAR_MASS = <NUM_LIT:4> * PI * PI <EOL> DAYS_PER_YEAR = <NUM_LIT> <EOL> BODIES = { <EOL> '<STR_LIT>' : ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] , [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] , SOLAR_MASS ) , <EOL> '<STR_LIT>' : ( [ <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> ] , <EOL> [ <NUM_LIT> * DAYS_PER_YEAR , <EOL> <NUM_LIT> * DAYS_PER_YEAR , <EOL> - <NUM_LIT> * DAYS_PER_YEAR ] , <EOL> <NUM_LIT> * SOLAR_MASS ) , <EOL> '<STR_LIT>' : ( [ <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> - <NUM_LIT> ] , <EOL> [ - <NUM_LIT> * DAYS_PER_YEAR , <EOL> <NUM_LIT> * DAYS_PER_YEAR , <EOL> <NUM_LIT> * DAYS_PER_YEAR ] , <EOL> <NUM_LIT> * SOLAR_MASS ) , <EOL> '<STR_LIT>' : ( [ <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> ] , <EOL> [ <NUM_LIT> * DAYS_PER_YEAR , <EOL> <NUM_LIT> * DAYS_PER_YEAR , <EOL> - <NUM_LIT> * DAYS_PER_YEAR ] , <EOL> <NUM_LIT> * SOLAR_MASS ) , <EOL> '<STR_LIT>' : ( [ <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> <NUM_LIT> ] , <EOL> [ <NUM_LIT> * DAYS_PER_YEAR , <EOL> <NUM_LIT> * DAYS_PER_YEAR , <EOL> - <NUM_LIT> * DAYS_PER_YEAR ] , <EOL> <NUM_LIT> * SOLAR_MASS ) } <EOL> def advance ( dt , n , bodies , pairs ) : <EOL> for i in range ( n ) : <EOL> for pair in pairs : <EOL> p1 , p2 = pair <EOL> vec1 , v1 , m1 = p1 <EOL> vec2 , v2 , m2 = p2 <EOL> x1 , y1 , z1 = vec1 <EOL> x2 , y2 , z2 = vec2 <EOL> dx = x1 - x2 <EOL> dy = y1 - y2 <EOL> dz = z1 - z2 <EOL> mag = dt * ( ( dx * dx + dy * dy + dz * dz ) ** ( - <NUM_LIT> ) ) <EOL> b1m = m1 * mag <EOL> b2m = m2 * mag <EOL> v1 [ <NUM_LIT:0> ] -= dx * b2m <EOL> v1 [ <NUM_LIT:1> ] -= dy * b2m <EOL> v1 [ <NUM_LIT:2> ] -= dz * b2m <EOL> v2 [ <NUM_LIT:0> ] += dx * b1m <EOL> v2 [ <NUM_LIT:1> ] += dy * b1m <EOL> v2 [ <NUM_LIT:2> ] += dz * b1m <EOL> for w in bodies : <EOL> r , v , m = w <EOL> r [ <NUM_LIT:0> ] += dt * v [ <NUM_LIT:0> ] <EOL> r [ <NUM_LIT:1> ] += dt * v [ <NUM_LIT:1> ] <EOL> r [ <NUM_LIT:2> ] += dt * v [ <NUM_LIT:2> ] <EOL> def report_energy ( bodies , pairs ) : <EOL> e = <NUM_LIT:0.0> <EOL> for pair in pairs : <EOL> p1 , p2 = pair <EOL> vec1 , v1 , m1 = p1 <EOL> vec2 , v2 , m2 = p2 <EOL> x1 , y1 , z1 = vec1 <EOL> x2 , y2 , z2 = vec2 <EOL> dx = x1 - x2 <EOL> dy = y1 - y2 <EOL> dz = z1 - z2 <EOL> e -= ( m1 * m2 ) / ( ( dx * dx + dy * dy + dz * dz ) ** <NUM_LIT:0.5> ) <EOL> for w in bodies : <EOL> r , v , m = w <EOL> vx , vy , vz = v <EOL> a = vx * vx + vy * vy + vz * vz <EOL> a /= <NUM_LIT> <EOL> e += m * a <EOL> return e <EOL> def offset_momentum ( ref , bodies , px = <NUM_LIT:0.0> , py = <NUM_LIT:0.0> , pz = <NUM_LIT:0.0> ) : <EOL> for w in bodies : <EOL> r , v , m = w <EOL> vx , vy , vz = v <EOL> px -= vx * m <EOL> py -= vy * m <EOL> pz -= vz * m <EOL> ( r , v , m ) = ref <EOL> v [ <NUM_LIT:0> ] = px / m <EOL> v [ <NUM_LIT:1> ] = py / m <EOL> v [ <NUM_LIT:2> ] = pz / m <EOL> def test_nbody ( iterations ) : <EOL> SYSTEM = [ ] <EOL> for key in BODIES : SYSTEM . append ( BODIES [ key ] ) <EOL> PAIRS = combinations ( SYSTEM ) <EOL> times = [ ] <EOL> for _ in range ( iterations ) : <EOL> t0 = time ( ) <EOL> start_e = report_energy ( SYSTEM , PAIRS ) <EOL> advance ( <NUM_LIT> , <NUM_LIT> , SYSTEM , PAIRS ) <EOL> end_e = report_energy ( SYSTEM , PAIRS ) <EOL> t1 = time ( ) <EOL> print ( '<STR_LIT>' , start_e ) <EOL> print ( '<STR_LIT>' , end_e ) <EOL> times . append ( t1 - t0 ) <EOL> return times </s>
