From 419a9502bad6b79dbd5f062023bd938fbb9ad7cc Mon Sep 17 00:00:00 2001 From: Julian Date: Sun, 7 Oct 2018 23:26:00 +0200 Subject: [PATCH 1/2] add verlet integration in golang --- .../verlet_integration/code/golang/verlet.go | 60 +++++++++++++++++++ .../verlet_integration/verlet_integration.md | 8 +++ 2 files changed, 68 insertions(+) create mode 100644 contents/verlet_integration/code/golang/verlet.go diff --git a/contents/verlet_integration/code/golang/verlet.go b/contents/verlet_integration/code/golang/verlet.go new file mode 100644 index 000000000..778521da4 --- /dev/null +++ b/contents/verlet_integration/code/golang/verlet.go @@ -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) +} diff --git a/contents/verlet_integration/verlet_integration.md b/contents/verlet_integration/verlet_integration.md index fcdc6a1a6..a9279efe7 100644 --- a/contents/verlet_integration/verlet_integration.md +++ b/contents/verlet_integration/verlet_integration.md @@ -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="golang" %} +[import:5-16, lang:"Golang"](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 @@ -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="golang" %} +[import:18-30, lang:"Golang"](code/golang/verlet.go) {% endmethod %} @@ -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="golang" %} +[import:32-42, lang:"Golang"](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))$$. @@ -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="golang" %} +[import, lang:"Golang"](code/golang/verlet.go) {% endmethod %} From fcf4f1a43b4845c1159584d919187cc61e8964a6 Mon Sep 17 00:00:00 2001 From: Julian Date: Tue, 9 Oct 2018 18:38:15 +0200 Subject: [PATCH 2/2] change the name golang to go --- .../verlet_integration/verlet_integration.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/contents/verlet_integration/verlet_integration.md b/contents/verlet_integration/verlet_integration.md index a9279efe7..7914bfa9e 100644 --- a/contents/verlet_integration/verlet_integration.md +++ b/contents/verlet_integration/verlet_integration.md @@ -59,8 +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="golang" %} -[import:5-16, lang:"Golang"](code/golang/verlet.go) +{% 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 @@ -107,8 +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="golang" %} -[import:18-30, lang:"Golang"](code/golang/verlet.go) +{% sample lang="go" %} +[import:18-30, lang:"go"](code/golang/verlet.go) {% endmethod %} @@ -169,8 +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="golang" %} -[import:32-42, lang:"Golang"](code/golang/verlet.go) +{% 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))$$. @@ -213,8 +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="golang" %} -[import, lang:"Golang"](code/golang/verlet.go) +{% sample lang="go" %} +[import, lang:"go"](code/golang/verlet.go) {% endmethod %}