{"id":27135631,"url":"https://github.com/peter-parit/n-body-gravity-sim","last_synced_at":"2026-02-16T20:02:37.264Z","repository":{"id":283296789,"uuid":"947383575","full_name":"peter-parit/n-body-gravity-sim","owner":"peter-parit","description":"An n-body simulation to compare performance and run-time between a naive algorithm to the Barnes-Hut algorithm. Uses ScalaFX for visualization.","archived":false,"fork":false,"pushed_at":"2025-04-03T18:31:03.000Z","size":122,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-03T19:32:08.917Z","etag":null,"topics":["concurrent-programming","gravity","nbody-simulation","parallel-computing","physics","scala","scalafx","simulation"],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/peter-parit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-12T15:49:00.000Z","updated_at":"2025-04-03T18:35:46.000Z","dependencies_parsed_at":"2025-04-03T19:25:57.391Z","dependency_job_id":"39e773f3-b9ed-478c-9483-fb3d86071743","html_url":"https://github.com/peter-parit/n-body-gravity-sim","commit_stats":null,"previous_names":["peter-parit/n-body-gravity-sim"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/peter-parit/n-body-gravity-sim","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peter-parit%2Fn-body-gravity-sim","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peter-parit%2Fn-body-gravity-sim/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peter-parit%2Fn-body-gravity-sim/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peter-parit%2Fn-body-gravity-sim/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/peter-parit","download_url":"https://codeload.github.com/peter-parit/n-body-gravity-sim/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peter-parit%2Fn-body-gravity-sim/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29516932,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-16T18:37:19.720Z","status":"ssl_error","status_checked_at":"2026-02-16T18:36:46.920Z","response_time":115,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["concurrent-programming","gravity","nbody-simulation","parallel-computing","physics","scala","scalafx","simulation"],"created_at":"2025-04-08T01:49:05.409Z","updated_at":"2026-02-16T20:02:37.241Z","avatar_url":"https://github.com/peter-parit.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"# N-body Gravity Simulation\n\n## Overview and Summary of Project Results\n\nhttps://github.com/user-attachments/assets/3b6622fe-dfea-4c33-b69b-e2719b4a2426\n\nThis n-body simulation will focus on simulating gravitational forces on n-objects on a 2D-plane. The purpose is to provide insights on the improved performance and run-time speed between the $O(n^2)$ naive approach to the $O(n\\log n)$ Barnes-Hut approach. The latter further utilizes a QuadTree data structure, along with concurrency such as scala's own Futures. ScalaFX is further used to visualize and simulate the programs.\n\n### Barnes-Hut\n\nBarnes-Hut Algorithm utilizes the ratio $\\frac{s}{d} \\leq \\Theta$, where $s$ is the width of the region, $d$ is the distance between two objects, and $\\Theta$ is the threshold used to determine whether Barnes-Hut or sequential is used. The Barnes-Hut approach is essentially obtaining a tree's center of mass and use it for force calculation instead of doing all individual bodies in that area, since the bodies are far away enough to not have any influence on the observed body.\n\n### Using Futures\n\nTo speed the computation time, Future was wrapped around the outer `map` (where `calculateForce` was called on every body), so all calculations are done concurrently. This resulted in roughly `2x` time faster computation time with bodies $\u003e 1,000,000$.\n\n### Results (computation time for `10` iterations)\n\nNUM_BODIES | NAIVE | BARNES-HUT | BARNES-HUT + FUTURE\n-----------|-------|------------|--------------------\n100 | 0.063 | 0.070 | 0.068\n500 | 0.122 | 0.083 | 0.077\n1,000 | 0.272 | 0.096 | 0.113\n5,000 | 3.275 | 0.243 | 0.233\n10,000 | 15.407 | 0.495 | 0.325\n50,000 | - | 1.799 | 0.984\n100,000 | - | 3.217 | 1.760\n1,000,000 | - | 41.089 | 26.945\n\nThe naive approach took extremely long for bodies of 10,000 or more. So, I have decided to not record down the time, and will proceed to use the existing values and predict the expected run time for each number of bodies in the graph section. Both Barnes-Hut versions are comparable up to 5,000 bodies, where the Future version grew lesser than the one without Futures. To provide more information, I also added the 1,000,000 body runtime, and it can be seen clearly that the Future version grew much slower.\n\n### Emperical Analysis\n\n- `red - naive`\n\n- `blue - barnes-hut`\n\n- `green - barnes-hut + future`\n\n![Image](https://github.com/user-attachments/assets/39df8a90-1243-4fe6-a46d-12c1031ba22f)\n\nAs expected, the naive approach grows in $O(n^2) time, while the other two grow in $O(n\\log n)$ time. Furthermore, it can be seen that the use of concurrent computation allows the computation speed to be moderately improved over the version without it.\n\nRead more about how results were taken in my report attached in the repository.\n\n## How to run simulation\nThis project is done using the SBT build tool, so you must import the build first. After that, you can run the simulation using `sbt run`.\n\n**Note**: Currently, the main running class is set to the `BarnesHutSim.scala`, which includes all optimized methods. If you want to try to run other main classes, namely `BarnesHutWithoutParSim` or `NaiveSim`, you must remove the following line from the `build.sbt` file in the project:\n\n`Compile / mainClass := Some(\"barneshut.BarnesHutSim\"),`\n\nSince I've imported dependencies for JavaFX, you shouldn't need to install them. However, if it doesn't work, then the version that is being used is the [SDK version 21](https://gluonhq.com/products/javafx/)\n\n*Keep in mind* that this method of visualization is not the fastest, so you might need a more powerful machine if you wanted to try simulating (with good fps) higher number of bodies i.e. 100,000+\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeter-parit%2Fn-body-gravity-sim","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpeter-parit%2Fn-body-gravity-sim","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeter-parit%2Fn-body-gravity-sim/lists"}