Skip to content

Commit 51543db

Browse files
Julianzsparal
Julian
authored andcommitted
Add verlet integration in golang (#484)
* add verlet integration in golang * change the name golang to go
1 parent 347b486 commit 51543db

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func verlet(pos, acc, dt float64) (time float64) {
6+
prevPos := pos
7+
time = 0
8+
9+
for pos > 0 {
10+
time += dt
11+
nextPos := pos*2 - prevPos + acc*dt*dt
12+
prevPos, pos = pos, nextPos
13+
}
14+
15+
return
16+
}
17+
18+
func stormerVerlet(pos, acc, dt float64) (time, vel float64) {
19+
prevPos := pos
20+
time, vel = 0, 0
21+
22+
for pos > 0 {
23+
time += dt
24+
vel += acc * dt
25+
nextPos := pos*2 - prevPos + acc*dt*dt
26+
prevPos, pos = pos, nextPos
27+
}
28+
29+
return
30+
}
31+
32+
func velocityVerlet(pos, acc, dt float64) (time, vel float64) {
33+
time, vel = 0, 0
34+
35+
for pos > 0 {
36+
time += dt
37+
pos += vel*dt + .5*acc*dt*dt
38+
vel += acc * dt
39+
}
40+
41+
return
42+
}
43+
44+
func main() {
45+
time := verlet(5., -10., .01)
46+
fmt.Println("Verlet")
47+
fmt.Println("Time:", time)
48+
fmt.Println()
49+
50+
time, vel := stormerVerlet(5., -10., .01)
51+
fmt.Println("Stormer-Verlet")
52+
fmt.Println("Time:", time)
53+
fmt.Println("Velocity:", vel)
54+
fmt.Println()
55+
56+
time, vel = velocityVerlet(5., -10., .01)
57+
fmt.Println("Velocity Verlet")
58+
fmt.Println("Time:", time)
59+
fmt.Println("Velocity:", vel)
60+
}

contents/verlet_integration/verlet_integration.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod
5959
[import:1-15, lang:"swift"](code/swift/verlet.swift)
6060
{% sample lang="f90" %}
6161
[import:1-20, lang:"fortran"](code/fortran/verlet.f90)
62+
{% sample lang="go" %}
63+
[import:5-16, lang:"go"](code/golang/verlet.go)
6264
{% endmethod %}
6365

6466
Now, obviously this poses a problem; what if we want to calculate a term that requires velocity, like the kinetic energy, $$\frac{1}{2}mv^2$$? In this case, we certainly cannot get rid of the velocity! Well, we can find the velocity to $$\mathcal{O}(\Delta t^2)$$ accuracy by using the Stormer-Verlet method, which is the same as before, but we calculate velocity like so
@@ -105,6 +107,8 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod
105107
[import:17-34, lang:"swift"](code/swift/verlet.swift)
106108
{% sample lang="f90" %}
107109
[import:22-42, lang:"fortran"](code/fortran/verlet.f90)
110+
{% sample lang="go" %}
111+
[import:18-30, lang:"go"](code/golang/verlet.go)
108112
{% endmethod %}
109113

110114

@@ -165,6 +169,8 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod
165169
[import:36-49, lang:"swift"](code/swift/verlet.swift)
166170
{% sample lang="f90" %}
167171
[import:44-60, lang:"fortran"](code/fortran/verlet.f90)
172+
{% sample lang="go" %}
173+
[import:32-42, lang:"go"](code/golang/verlet.go)
168174
{% endmethod %}
169175

170176
Even though this method is more widely used than the simple Verlet method mentioned above, it unforunately has an error term of $$\mathcal{O}(\Delta t^2)$$, which is two orders of magnitude worse. That said, if you want to have a simulaton with many objects that depend on one another --- like a gravity simulation --- the Velocity Verlet algorithm is a handy choice; however, you may have to play further tricks to allow everything to scale appropriately. These types of simulatons are sometimes called *n-body* simulations and one such trick is the Barnes-Hut algorithm, which cuts the complexity of n-body simulations from $$\sim \mathcal{O}(n^2)$$ to $$\sim \mathcal{O}(n\log(n))$$.
@@ -207,6 +213,8 @@ Submitted by P. Mekhail
207213
[import, lang:"swift"](code/swift/verlet.swift)
208214
{% sample lang="f90" %}
209215
[import, lang:"fortran"](code/fortran/verlet.f90)
216+
{% sample lang="go" %}
217+
[import, lang:"go"](code/golang/verlet.go)
210218
{% endmethod %}
211219

212220

0 commit comments

Comments
 (0)