nbody.jl 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # StarPU --- Runtime system for heterogeneous multicore architectures.
  2. #
  3. # Copyright (C) 2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. #
  5. # StarPU is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU Lesser General Public License as published by
  7. # the Free Software Foundation; either version 2.1 of the License, or (at
  8. # your option) any later version.
  9. #
  10. # StarPU is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. #
  14. # See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. #
  16. include("nbody_display.jl")
  17. function mod2(v ::Vector{Float64})
  18. return sqrt(v[1]^2 + v[2]^2)
  19. end
  20. function compute_accelerations(positions ::Vector{Vector{Float64}}, accelerations ::Vector{Vector{Float64}}, masses ::Vector{Float64}, eps ::Float64)
  21. G ::Float64 = 6.67E-11
  22. n ::Int64 = length(accelerations)
  23. for i = 1:n
  24. sumacc ::Vector{Float64} = [0,0]
  25. for j = 1:n
  26. if i != j
  27. dv ::Vector{Float64} = positions[j] - positions[i]
  28. sumacc = sumacc + G * masses[j] * dv / (mod2(dv) + eps)^3
  29. end
  30. end
  31. accelerations[i] = sumacc
  32. end
  33. end
  34. function update_pos_vel(positions ::Vector{Vector{Float64}}, velocities ::Vector{Vector{Float64}}, accelerations ::Vector{Vector{Float64}}, dt ::Float64)
  35. n ::Int64 = length(positions)
  36. for i = 1:n
  37. velocities[i] = velocities[i] + accelerations[i] * dt
  38. positions[i] = positions[i] + velocities[i] * dt
  39. end
  40. end
  41. function nbody_jl(positions ::Vector{Vector{Float64}}, velocities ::Vector{Vector{Float64}}, accelerations ::Vector{Vector{Float64}}, masses ::Vector{Float64}, nbr_simulations ::Int64, eps ::Float64, dt ::Float64)
  42. for i = 1:nbr_simulations
  43. compute_accelerations(positions, accelerations, masses, eps)
  44. update_pos_vel(positions, velocities, accelerations,dt)
  45. # pixels ::Array{Array{Int64}} = [[0,0,0] for i = 1:1000*1000]
  46. # graph_planets(pixels, positions, -4E8, 4E8, 1000, 1000, "nbody$i.ppm")
  47. end
  48. end