Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F61106267
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
Sat, May 4, 14:09
Size
990 B
Mime Type
text/x-c
Expires
Mon, May 6, 14:09 (2 d)
Engine
blob
Format
Raw Data
Handle
17390667
Attached To
R9484 sp4e-homework-lars-bertil
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