# Gravitation

No preview image

### 1 collaborator

Michael Kushnir (Author)

### Tags

(This model has yet to be categorized with any tags)
Parent of 2 models: Gravity Wells and Orbital Playground
Model group MAM-2016 | Visible to everyone | Changeable by the author
Model was written in NetLogo 3D 6.0-M5 • Viewed 296 times • Downloaded 25 times • Run 0 times

## WHAT IS IT?

This model is part of the Gravitational Model Suite. This is the most basic version of the model, in which particles exert gravitational force upon each other.

## HOW IT WORKS

Every particle has mass, position, and velocity. At every time step, every particle pulls on every other particle with a gravitational force proportional to its mass and inversely proportional to the distance between the two particles. Each particle sums up these forces to calculate its total acceleration at every tick, and updates its velocity accordingly. These rules are simply an implementation of well known Newtonian laws of physics - but some interesting behaviors can result.

## HOW TO USE IT

The NUMBER-OF-OBJECTS slider determines how many particles are spawned. The STARTING-BOX slider defines the coordinates they can be spawned inside - a smaller value spawns them closer to the center. MAX-STARTING-VELOCITY allows them to have a randomly determined starting velocity, and MAX-STARTING-MASS defines a maximum mass for the particles to be randomly assigned. SETUP sets up the model, and GO starts or pauses it. SETUP-TWO-ORBITS, SETUP-HELIOCENTRIC-ORBIT, and SETUP-4 all load up interesting starting configurations. PARTICLE-SCALE increases the size of the particles, for easier viewing when the model is very zoomed out.

## THINGS TO NOTICE

Notice that along an orbit, kinetic energy and potential energy are in opposition. When a particle reaches the highest point in its orbit (i.e., has the most potential energy), it is moving the slowest. When the particle reaches its lowest point, it is moving fastest. Conservation of energy states that a particle's total energy (kinetic + potential) must be constant unless it gains or loses energy by interaction with something else. Therefore, as it is moving through its orbit, it converts kinetic energy to potential energy and vice versa.

This also means that if (for example) a particle gains kinetic energy at the lowest point of its orbit, its highest point will correspondingly have more potential energy, which requires it to be farther away. In simpler terms, speeding up widens the opposite end of the orbit, and slowing down brings the opposite end closer. These are all emergent properties that result naturally from Newtonian laws of gravitation.

## EXTENDING THE MODEL

There are plenty of ways that this could be made quicker. Since every particle interacts with every other particle, this model is O(N^2), which is pretty bad. One simplification would be to only consider particles within a certain distance, assuming once you get too far away the gravitational pull is negligible. Alternatively, groups of particles could be considered together in calculations to speed things up. However, I decided to leave it as is for now, favoring accuracy over simplifications for the sake of speed.

## NETLOGO FEATURES

One workaround that was used was "zooming" in and out to compensate for the fact that Netlogo doesn't support resizing the world. So when a particle tries to go outside the bounds of the world, rather than making the world bigger, I just scaled down all the positions. This is why every turtle has a separate x-pos, y-pos and z-pos defined in addition to the default xcor, ycor and zcor; the new -pos variables are not scaled down and are used in all physics calculations, while the default cor variables are modified by the global zoom-factor variable and are used for displaying turtles. As a result, the world can effectively become bigger and smaller as needed.

## RELATED MODELS

This model is part of a suite of models, along with the Gravity Wells model and the Orbital Playground model.

## CREDITS AND REFERENCES

http://modelingcommons.org/browse/one_model/4623

Wilensky, U. (1999). NetLogo [computer software]. Evanston, IL: Center for Connected Learning and Computer-Based Modeling, Northwestern University. http://ccl.northwestern.edu/netlogo .

Click to Run Model

```turtles-own [ x-velocity y-velocity z-velocity ; 3d velocity of the particle
x-pos y-pos z-pos ; physical position of the particle
mass ; represents physical mass. More mass means a greater gravitational pull
]
globals [
zoom-factor ; used to relate x, y, z-pos variables to actual xcor, ycor, zcor
]

to setup
clear-all
set-default-shape turtles "circle"
set zoom-factor 1
create-turtles number-of-objects [
setxyz (random-float starting-box - random-float starting-box) (random-float starting-box - random-float starting-box) (random-float starting-box - random-float starting-box)
set x-velocity ((random-float max-starting-velocity) - (random-float max-starting-velocity))
set y-velocity ((random-float max-starting-velocity) - (random-float max-starting-velocity))
set z-velocity ((random-float max-starting-velocity) - (random-float max-starting-velocity))
set mass random-float max-starting-mass
set size mass ^ (1 / 3)

set x-pos xcor
set y-pos ycor
set z-pos zcor
]
reset-ticks
end

;; Sets up a symmetric system of two particles orbiting each other

to setup-two-orbits
clear-all
set-default-shape turtles "circle"
set zoom-factor 1
create-turtles 1 [
setxyz 5 0 0
set x-velocity .03
set y-velocity .1
set z-velocity 0
set mass 3
set size mass ^ (1 / 3)
set x-pos xcor
set y-pos ycor
set z-pos zcor
]
create-turtles 1 [
setxyz -5 0 0
set x-velocity 0
set y-velocity -.1
set z-velocity 0
set mass 3
set size mass ^ (1 / 3)
set x-pos xcor
set y-pos ycor
set z-pos zcor
]
reset-ticks
end

; Sets up a system with a "sun" and a smaller particle orbiting it

to setup-heliocentric-orbit
clear-all
set-default-shape turtles "circle"
set zoom-factor 1
create-turtles 1 [
setxyz 0 0 0
set x-velocity 0
set y-velocity 0
set z-velocity 0
set mass 1000
set size mass ^ (1 / 3)
set x-pos xcor
set y-pos ycor
set z-pos zcor
]
create-turtles 1 [
setxyz -20 0 0
set x-velocity 0
set y-velocity 2
set z-velocity 0
set mass 1
set size mass
set x-pos xcor
set y-pos ycor
set z-pos zcor
]
reset-ticks
end

; Sets up a system of two pairs of particles - each particle orbits the other in its pair, and the pairs orbit each other

to setup-4
clear-all
set-default-shape turtles "circle"
set zoom-factor 1
create-turtles 1 [
setxyz 23 5 0
set x-velocity .07
set y-velocity -.05
set z-velocity 0
set mass 2
set size mass ^ (1 / 3)
set x-pos xcor
set y-pos ycor
set z-pos zcor
]
create-turtles 1 [
setxyz 23 -5 0
set x-velocity -.07
set y-velocity -.05
set z-velocity 0
set mass 2
set size mass ^ (1 / 3)
set x-pos xcor
set y-pos ycor
set z-pos zcor
]
create-turtles 1 [
setxyz -23 5 0
set x-velocity .07
set y-velocity .05
set z-velocity 0
set mass 2
set size mass ^ (1 / 3)
set x-pos xcor
set y-pos ycor
set z-pos zcor
]
create-turtles 1 [
setxyz -23 -5 0
set x-velocity -.07
set y-velocity .05
set z-velocity 0
set mass 2
set size mass ^ (1 / 3)
set x-pos xcor
set y-pos ycor
set z-pos zcor
]
reset-ticks
end

to go
compute-acceleration-changes
check-boundaries
move
check-slide
tick
end

; Keeps the world centered on the center of mass of all particles

to check-slide
let CoM center-of-mass turtles
slide-view (item 0 CoM) (item 1 CoM) (item 2 CoM)
end

; Calculates the center of mass of a turtleset

to-report center-of-mass [turtleset]
; R = 1/M sigma (mi * ri)
let M 0
let totalx 0
let totaly 0
let totalz 0
let mi [mass] of self
set M M + mi
set totalx totalx + [x-pos] of self * mi
set totaly totaly + [y-pos] of self * mi
set totalz totalz + [z-pos] of self * mi
]

report (list (totalx / M) (totaly / M) (totalz / M))
end

; Checks turtle positions in the world, zooms out if any turtle is too close to the edge

to check-boundaries
let flag false
if abs (x-pos + x-velocity) > max-pxcor * zoom-factor * .8 or
abs (y-pos + y-velocity) > max-pycor * zoom-factor * .8 or
abs (z-pos + z-velocity) > max-pzcor * zoom-factor * .8
[
set flag true
]
]

if flag [
print "zooming out"
set-zoom 1.02
]

set flag false
if abs (x-pos + x-velocity) > max-pxcor * zoom-factor * .5 or
abs (y-pos + y-velocity) > max-pycor * zoom-factor * .5 or
abs (z-pos + z-velocity) > max-pzcor * zoom-factor * .5
[
set flag true
]
]
if (not flag) [
;print "zooming in"
;set-zoom .98
]
end

to tick-once
compute-acceleration-changes
check-boundaries
move
check-slide
tick
end

; Asks turtles to gravitationally affect each other

to compute-acceleration-changes
let turtle1 self
gravitationally-affect turtle1 self
]
]
end

; Update turtle positions based on velocity

to move
set x-pos x-pos + x-velocity
set y-pos y-pos + y-velocity
set z-pos z-pos + z-velocity

if not hidden? [
set xcor x-pos / zoom-factor
set ycor y-pos / zoom-factor
set zcor z-pos / zoom-factor
]
]
end

; Calculates gravitational pull of one turtle upon another

to gravitationally-affect [ affected affecting ]
; g = m1 * m2 / r^2
; r = sqrt(xdist^2 + ydist^2) -> r^2 = xdist^2 + ydist^2
let xdist [x-pos] of affecting - [x-pos] of affected
;print xdist
let ydist [y-pos] of affecting - [y-pos] of affected
;print ydist
let zdist [z-pos] of affecting - [z-pos] of affected
let total-acc 0
let xyangle 90
let zangle 90
let xydist (xdist ^ 2 + ydist ^ 2) ^ .5
if abs(xdist) + abs(ydist) + abs(zdist) > 0 [
set total-acc [mass] of affecting / max (list (xdist ^ 2 + ydist ^ 2 + zdist ^ 2) ((([size] of affected + [size] of affecting) / 2) ^ 2))
set xyangle (90 - angle-heading) mod 360
set zangle (90 - z-angle-heading) mod 360
]

if total-acc > 50 [ set total-acc 50 ]

set total-acc total-acc / 20
let xyacc total-acc * (cos zangle)

set x-velocity (x-velocity + xyacc * (cos xyangle))
set y-velocity (y-velocity + xyacc * (sin xyangle))
set z-velocity (z-velocity + total-acc * (sin zangle))
]
end

; Shifts positions of all turtles, used to stay centered around center of mass

to slide-view [deltax deltay deltaz]
set x-pos x-pos - deltax
set y-pos y-pos - deltay
set z-pos z-pos - deltaz
]
;print "slide"
end

; Zooms scale of the world in or out

to set-zoom [new-zoom]
set zoom-factor zoom-factor * new-zoom
print zoom-factor
ask turtles [set size mass ^ (1 / 3) / zoom-factor * particle-scale]
end
```

There are 5 versions of this model.