Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F79572713
compute_gravity.cc
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Tue, Aug 27, 04:13
Size
992 B
Mime Type
text/x-c
Expires
Thu, Aug 29, 04:13 (2 d)
Engine
blob
Format
Raw Data
Handle
20066753
Attached To
R7561 SP4E_HW1
compute_gravity.cc
View Options
#include "compute_gravity.hh"
#include <cmath>
/* -------------------------------------------------------------------------- */
void
ComputeGravity
::
compute
(
System
&
system
)
{
// define a lambda function for newtonian gravity
auto
newton_gravity
=
[
this
](
Particle
&
par1
,
Particle
&
par2
)
{
// compute the distance vector and the square of distance
auto
v_r
=
par2
.
getPosition
()
-
par1
.
getPosition
();
auto
r2
=
v_r
.
squaredNorm
();
if
(
r2
==
0.
)
return
;
// compute the distance
auto
r
=
sqrt
(
r2
);
v_r
*=
1
/
r
;
// compute the pair force
auto
force
=
par1
.
getMass
()
*
par2
.
getMass
()
*
G
/
r2
*
v_r
;
// add up the force for both concerned particles
par2
.
getForce
()
-=
force
;
par1
.
getForce
()
+=
force
;
};
// apply newtonian gravity on all pairs of particles
applyOnPairs
(
newton_gravity
,
system
);
}
/* -------------------------------------------------------------------------- */
void
ComputeGravity
::
setG
(
Real
G
)
{
this
->
G
=
G
;
}
Event Timeline
Log In to Comment