v/examples/nbody.v

133 lines
3.7 KiB
V
Raw Normal View History

2019-07-06 07:49:16 +02:00
// Ported based on https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/nbody-go-3.html
// Output:
// -0.169075164
// -0.169059907
import math
const (
2020-05-22 17:36:09 +02:00
solar_mass = 39.47841760435743197 //4.0 * math.Pi * math.Pi
days_per_year = 365.24
c_n = 5
2019-07-06 07:49:16 +02:00
)
struct Position {
pub mut:
x f64
y f64
z f64
}
struct Momentum {
pub mut:
x f64
y f64
z f64
m f64
}
struct System {
pub mut:
v []Momentum
s []Position
}
fn advance(sys mut System, dt f64) {
2020-05-22 17:36:09 +02:00
for i in 0..c_n - 1 {
2020-06-02 16:09:41 +02:00
mut vx := sys.v[i].x
mut vy := sys.v[i].y
mut vz := sys.v[i].z
2019-07-06 07:49:16 +02:00
2020-05-22 17:36:09 +02:00
for j := i + 1; j < c_n; j++ {
2019-07-06 07:49:16 +02:00
dx := sys.s[i].x - sys.s[j].x
dy := sys.s[i].y - sys.s[j].y
dz := sys.s[i].z - sys.s[j].z
2019-07-15 22:44:26 +02:00
dsquared := dx * dx + dy * dy + dz * dz
distance := math.sqrt(dsquared)
mag := (dt / (dsquared * distance))
2019-07-06 07:49:16 +02:00
mi := sys.v[i].m
2020-06-02 16:09:41 +02:00
vx -= dx * sys.v[j].m * mag
vy -= dy * sys.v[j].m * mag
vz -= dz * sys.v[j].m * mag
2019-07-06 07:49:16 +02:00
sys.v[j].x += dx * mi * mag
sys.v[j].y += dy * mi * mag
sys.v[j].z += dz * mi * mag
}
2020-06-02 16:09:41 +02:00
sys.v[i].x = vx
sys.v[i].y = vy
sys.v[i].z = vz
2019-07-06 07:49:16 +02:00
}
2020-05-22 17:36:09 +02:00
for i in 0..c_n {
2019-07-06 07:49:16 +02:00
sys.s[i].x += dt * sys.v[i].x
sys.s[i].y += dt * sys.v[i].y
sys.s[i].z += dt * sys.v[i].z
}
}
2019-08-07 14:16:10 +02:00
fn offsetmomentum(sys mut System) {
2019-07-06 07:49:16 +02:00
mut px := f64(0)
mut py := f64(0)
mut pz := f64(0)
2020-05-22 17:36:09 +02:00
for i in 0..c_n {
2019-07-06 07:49:16 +02:00
px += sys.v[i].x * sys.v[i].m
py += sys.v[i].y * sys.v[i].m
pz += sys.v[i].z * sys.v[i].m
}
2020-05-22 17:36:09 +02:00
sys.v[0].x = -px / solar_mass
sys.v[0].y = -py / solar_mass
sys.v[0].z = -pz / solar_mass
2019-07-06 07:49:16 +02:00
}
fn energy(sys System) f64 {
mut e := f64(0)
2020-05-22 17:36:09 +02:00
for i in 0..c_n {
2019-07-06 07:49:16 +02:00
e += 0.5 * sys.v[i].m * (sys.v[i].x * sys.v[i].x + sys.v[i].y * sys.v[i].y + sys.v[i].z * sys.v[i].z)
2020-05-22 17:36:09 +02:00
for j := i + 1; j < c_n; j++ {
2019-07-06 07:49:16 +02:00
dx := sys.s[i].x - sys.s[j].x
dy := sys.s[i].y - sys.s[j].y
dz := sys.s[i].z - sys.s[j].z
distance := math.sqrt(dx * dx + dy * dy + dz * dz)
e -= (sys.v[i].m * sys.v[j].m) / distance
}
}
return e
}
fn arr_momentum() []Momentum {
2019-07-07 01:18:04 +02:00
return [
2020-05-22 17:36:09 +02:00
Momentum {0.0, 0.0, 0.0, solar_mass},
Momentum {1.66007664274403694e-03 * days_per_year, 7.69901118419740425e-03 * days_per_year, -6.90460016972063023e-05 * days_per_year, 9.54791938424326609e-04 * solar_mass},
Momentum {-2.76742510726862411e-03 * days_per_year, 4.99852801234917238e-03 * days_per_year, 2.30417297573763929e-05 * days_per_year, 2.85885980666130812e-04 * solar_mass},
Momentum {2.96460137564761618e-03 * days_per_year, 2.37847173959480950e-03 * days_per_year, -2.96589568540237556e-05 * days_per_year, 4.36624404335156298e-05 * solar_mass},
Momentum {2.68067772490389322e-03 * days_per_year, 1.62824170038242295e-03 * days_per_year, -9.51592254519715870e-05 * days_per_year, 5.15138902046611451e-05 * solar_mass},
2019-07-07 01:18:04 +02:00
]
2019-07-06 07:49:16 +02:00
}
2020-06-02 16:09:41 +02:00
fn arr_position() []Position {
2019-07-07 01:18:04 +02:00
return [
Position {0.0, 0.0, 0.0},
Position {4.84143144246472090e+00, -1.16032004402742839e+00, -1.03622044471123109e-01},
Position {8.34336671824457987e+00, 4.12479856412430479e+00, -4.03523417114321381e-01},
Position {1.28943695621391310e+01, -1.51111514016986312e+01, -2.23307578892655734e-01},
Position {1.53796971148509165e+01, -2.59193146099879641e+01, 1.79258772950371181e-01},
]
2019-07-06 07:49:16 +02:00
}
fn main() {
sys := &System {arr_momentum(), arr_position()}
2019-08-07 14:16:10 +02:00
offsetmomentum(mut sys)
2019-07-06 07:49:16 +02:00
println('${energy(sys):.9f}') //-0.169075164
2020-05-12 13:57:23 +02:00
for _ in 0..50000000 {
advance(mut sys, 0.01)
2019-07-06 07:49:16 +02:00
}
println('${energy(sys):.9f}') //-0.169059907
}