Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
N Body Simulation
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Admin message
This server has been upgraded to GitLab release
17.10
.
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Armin Damon Riess
N Body Simulation
Commits
2d6b8ccf
Commit
2d6b8ccf
authored
2 years ago
by
Armin Damon Riess
Browse files
Options
Downloads
Patches
Plain Diff
prepared simulation to use tree
parent
06081d87
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
lib/nBodySim.cpp
+11
-33
11 additions, 33 deletions
lib/nBodySim.cpp
lib/nBodySim.hpp
+4
-0
4 additions, 0 deletions
lib/nBodySim.hpp
lib/tree.cpp
+10
-1
10 additions, 1 deletion
lib/tree.cpp
lib/tree.hpp
+6
-1
6 additions, 1 deletion
lib/tree.hpp
with
31 additions
and
35 deletions
lib/nBodySim.cpp
+
11
−
33
View file @
2d6b8ccf
#include
"nBodySim.hpp"
#include
"nBodySim.hpp"
#include
"tree.hpp"
#include
<omp.h>
#include
<omp.h>
#include
<cmath>
#include
<cmath>
#include
<string>
#include
<string>
...
@@ -15,10 +16,12 @@ nBodySim::nBodySim(std::string datafile) {
...
@@ -15,10 +16,12 @@ nBodySim::nBodySim(std::string datafile) {
file
>>
masses_
[
i
];
file
>>
masses_
[
i
];
}
}
double
maxCoord
=
0
;
positions_
=
new
double
[
3
*
nParticles_
];
positions_
=
new
double
[
3
*
nParticles_
];
for
(
unsigned
i
=
0
;
i
<
3
;
++
i
)
{
for
(
unsigned
i
=
0
;
i
<
3
;
++
i
)
{
for
(
unsigned
j
=
0
;
j
<
nParticles_
;
++
j
)
{
for
(
unsigned
j
=
0
;
j
<
nParticles_
;
++
j
)
{
file
>>
positions_
[
i
+
3
*
j
];
file
>>
positions_
[
i
+
3
*
j
];
if
(
std
::
abs
(
positions_
[
i
+
3
*
j
])
>
maxCoord
)
maxCoord
=
std
::
abs
(
positions_
[
i
+
3
*
j
]);
}
}
}
}
...
@@ -45,6 +48,8 @@ nBodySim::nBodySim(std::string datafile) {
...
@@ -45,6 +48,8 @@ nBodySim::nBodySim(std::string datafile) {
}
}
file
.
close
();
file
.
close
();
tree_
=
new
Tree
(
positions_
,
forces_
,
nParticles_
,
4
*
maxCoord
,
new
double
[
3
]{
0
,
0
,
0
});
}
}
nBodySim
::~
nBodySim
()
{
nBodySim
::~
nBodySim
()
{
...
@@ -107,38 +112,9 @@ void nBodySim::calculateForces() {
...
@@ -107,38 +112,9 @@ void nBodySim::calculateForces() {
}
}
}
}
/*
void
nBodySim
::
treeCalculateForces
()
{
// naive implementation
// TODO
void nBodySim::calculateForces() {
#pragma omp parallel for
for (unsigned i=0; i < 3*nParticles_; ++i) {
forces_[i] = 0.0;
}
// loop over each pair of particles and calculate the force between them
#pragma omp parallel for collapse(2)
for (unsigned i = 0; i < nParticles_; i++) {
for (unsigned j = 0; j < nParticles_; j++) {
if (i == j) continue;
// calculate distance between particles
double dx = positions_[3*j] - positions_[3*i];
double dy = positions_[3*j+1] - positions_[3*i+1];
double dz = positions_[3*j+2] - positions_[3*i+2];
double r2 = dx*dx + dy*dy + dz*dz;
double r = std::sqrt(r2);
dx /= r;
dy /= r;
dz /= r;
// calculate force
double mi = masses_[i];
double mj = masses_[j];
double s = softening_[i];
forces_[3*i] += mi*mj * dx / (r2 + s*s);
forces_[3*i+1] += mi*mj * dy / (r2 + s*s);
forces_[3*i+2] += mi*mj * dz / (r2 + s*s);
}
}
}
}
*/
double
nBodySim
::
calculateMeanInterparticleDistance
()
{
double
nBodySim
::
calculateMeanInterparticleDistance
()
{
meanInterparticleDistance_
=
0.0
;
meanInterparticleDistance_
=
0.0
;
...
@@ -184,8 +160,10 @@ void nBodySim::doTimeStep(double dt) {
...
@@ -184,8 +160,10 @@ void nBodySim::doTimeStep(double dt) {
for
(
unsigned
i
=
0
;
i
<
nParticles_
;
++
i
)
{
for
(
unsigned
i
=
0
;
i
<
nParticles_
;
++
i
)
{
for
(
unsigned
j
=
0
;
j
<
3
;
++
j
)
{
for
(
unsigned
j
=
0
;
j
<
3
;
++
j
)
{
// TODO
// TODO
// update velocity
tree_
->
drift
(
dt
/
2
);
// update position
tree_
->
update
();
tree_
->
kick
(
dt
);
tree_
->
drift
(
dt
/
2
);
}
}
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
lib/nBodySim.hpp
+
4
−
0
View file @
2d6b8ccf
#ifndef NBODYSIM_HPP
#ifndef NBODYSIM_HPP
#define NBODYSIM_HPP
#define NBODYSIM_HPP
#include
"tree.hpp"
#include
<string>
#include
<string>
class
nBodySim
{
class
nBodySim
{
...
@@ -13,6 +14,8 @@ public:
...
@@ -13,6 +14,8 @@ public:
void
runSimulation
(
double
dt
,
unsigned
nSteps
);
void
runSimulation
(
double
dt
,
unsigned
nSteps
);
// loops over all pairs of particles and calculates forces, calculates current mean interparticle distance
// loops over all pairs of particles and calculates forces, calculates current mean interparticle distance
void
calculateForces
();
void
calculateForces
();
// use treecode to calculate forces
void
treeCalculateForces
();
// loop over all pairs of particles and calculate mean interparticle distance
// loop over all pairs of particles and calculate mean interparticle distance
double
calculateMeanInterparticleDistance
();
double
calculateMeanInterparticleDistance
();
// loop over all force vectors and calculate mean force magnitude
// loop over all force vectors and calculate mean force magnitude
...
@@ -32,6 +35,7 @@ public:
...
@@ -32,6 +35,7 @@ public:
double
getMeanInterparticleDistance
()
const
{
return
meanInterparticleDistance_
;
}
double
getMeanInterparticleDistance
()
const
{
return
meanInterparticleDistance_
;
}
double
getMeanForceMagnitude
()
const
{
return
meanForceMagnitude_
;
}
double
getMeanForceMagnitude
()
const
{
return
meanForceMagnitude_
;
}
private
:
private
:
Tree
*
tree_
;
unsigned
nParticles_
;
unsigned
nParticles_
;
// 3d vectors are stored as a array of length 3*nParticles_ in the format x1, y1, z1, x2, y2, z2, ...
// 3d vectors are stored as a array of length 3*nParticles_ in the format x1, y1, z1, x2, y2, z2, ...
double
*
masses_
;
double
*
masses_
;
...
...
This diff is collapsed.
Click to expand it.
lib/tree.cpp
+
10
−
1
View file @
2d6b8ccf
#include
"tree.hpp"
#include
"tree.hpp"
#include
"node.hpp"
#include
"node.hpp"
Tree
::
Tree
(
double
*
particles
,
unsigned
nParticles
,
double
size
,
double
*
center
)
{
Tree
::
Tree
(
double
*
particles
,
double
*
forces
,
unsigned
nParticles
,
double
size
,
double
*
center
)
{
particles_
=
particles
;
particles_
=
particles
;
forces_
=
forces
;
nParticles_
=
nParticles
;
nParticles_
=
nParticles
;
size_
=
size
;
size_
=
size
;
...
@@ -21,4 +22,12 @@ Tree::~Tree() {
...
@@ -21,4 +22,12 @@ Tree::~Tree() {
void
Tree
::
update
()
{
void
Tree
::
update
()
{
// TODO
// TODO
}
void
Tree
::
drift
(
double
dt
)
{
// TODO
}
void
Tree
::
kick
(
double
dt
)
{
// TODO
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
lib/tree.hpp
+
6
−
1
View file @
2d6b8ccf
...
@@ -7,14 +7,19 @@ class Tree {
...
@@ -7,14 +7,19 @@ class Tree {
public:
public:
// constructor
// constructor
Tree
()
=
delete
;
Tree
()
=
delete
;
Tree
(
double
*
particles
,
unsigned
nParticles
,
double
size
,
double
*
center
);
Tree
(
double
*
particles
,
double
*
forces_
,
unsigned
nParticles
,
double
size
,
double
*
center
);
// destructor
// destructor
~
Tree
();
~
Tree
();
// update tree: visit each node, check if it needs to be split or merged (for example if a particle has left the region)
// update tree: visit each node, check if it needs to be split or merged (for example if a particle has left the region)
void
update
();
void
update
();
// drift tree: visit each node, update center of mass and center of mass velocity
void
drift
(
double
dt
);
// kick tree: visit each node, update center of mass velocity
void
kick
(
double
dt
);
private:
private:
Node
*
root_
;
Node
*
root_
;
double
*
particles_
;
double
*
particles_
;
double
*
forces_
;
unsigned
nParticles_
;
unsigned
nParticles_
;
double
size_
;
double
size_
;
double
center_
[
3
];
double
center_
[
3
];
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment