Skip to content

Add verlet integration in golang #484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions contents/verlet_integration/code/golang/verlet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import "fmt"

func verlet(pos, acc, dt float64) (time float64) {
prevPos := pos
time = 0

for pos > 0 {
time += dt
nextPos := pos*2 - prevPos + acc*dt*dt
prevPos, pos = pos, nextPos
}

return
}

func stormerVerlet(pos, acc, dt float64) (time, vel float64) {
prevPos := pos
time, vel = 0, 0

for pos > 0 {
time += dt
vel += acc * dt
nextPos := pos*2 - prevPos + acc*dt*dt
prevPos, pos = pos, nextPos
}

return
}

func velocityVerlet(pos, acc, dt float64) (time, vel float64) {
time, vel = 0, 0

for pos > 0 {
time += dt
pos += vel*dt + .5*acc*dt*dt
vel += acc * dt
}

return
}

func main() {
time := verlet(5., -10., .01)
fmt.Println("Verlet")
fmt.Println("Time:", time)
fmt.Println()

time, vel := stormerVerlet(5., -10., .01)
fmt.Println("Stormer-Verlet")
fmt.Println("Time:", time)
fmt.Println("Velocity:", vel)
fmt.Println()

time, vel = velocityVerlet(5., -10., .01)
fmt.Println("Velocity Verlet")
fmt.Println("Time:", time)
fmt.Println("Velocity:", vel)
}
8 changes: 8 additions & 0 deletions contents/verlet_integration/verlet_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod
[import:1-15, lang:"swift"](code/swift/verlet.swift)
{% sample lang="f90" %}
[import:1-20, lang:"fortran"](code/fortran/verlet.f90)
{% sample lang="go" %}
[import:5-16, lang:"go"](code/golang/verlet.go)
{% endmethod %}

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
Expand Down Expand Up @@ -105,6 +107,8 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod
[import:17-34, lang:"swift"](code/swift/verlet.swift)
{% sample lang="f90" %}
[import:22-42, lang:"fortran"](code/fortran/verlet.f90)
{% sample lang="go" %}
[import:18-30, lang:"go"](code/golang/verlet.go)
{% endmethod %}


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

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))$$.
Expand Down Expand Up @@ -207,6 +213,8 @@ Submitted by P. Mekhail
[import, lang:"swift"](code/swift/verlet.swift)
{% sample lang="f90" %}
[import, lang:"fortran"](code/fortran/verlet.f90)
{% sample lang="go" %}
[import, lang:"go"](code/golang/verlet.go)
{% endmethod %}


Expand Down