Skip to content

Added Verlet integration in Kotlin #535

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 6 commits into from
Oct 30, 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
55 changes: 55 additions & 0 deletions contents/verlet_integration/code/kotlin/verlet.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
data class VerletValues(val time: Double, val vel: Double)

fun verlet(_pos: Double, acc: Double, dt: Double): Double {
var pos = _pos // Since function parameter are val and can't be modified
var prevPos = pos
var time = 0.0

while (pos > 0) {
time += dt
val nextPos = pos * 2 - prevPos + acc * dt * dt
prevPos = pos
pos = nextPos
}
return time
}

fun stormerVerlet(_pos: Double, acc: Double, dt: Double): VerletValues {
var pos = _pos
var prevPos = pos
var time = 0.0
var vel = 0.0
while (pos > 0) {
time += dt
val nextPos = pos * 2 - prevPos + acc * dt * dt
prevPos = pos
pos = nextPos
vel += acc * dt
}
return VerletValues(time, vel)
}

fun velocityVerlet(_pos: Double, acc: Double, dt: Double): VerletValues {
var pos = _pos
var time = 0.0
var vel = 0.0
while (pos > 0) {
time += dt
pos += vel * dt + 0.5 * acc * dt * dt
vel += acc * dt
}
return VerletValues(time, vel)
}

fun main(args: Array<String>) {
val verletTime = verlet(5.0, -10.0, 0.01)
println("Time for Verlet integration is: $verletTime")

val stormerVerlet = stormerVerlet(5.0, -10.0, 0.01)
println("Time for Stormer Verlet integration is: $stormerVerlet.time")
println("Velocity for Stormer Verlet integration is: $stormerVerlet.vel")

val velocityVerlet = velocityVerlet(5.0, -10.0, 0.01)
println("Time for Velocity Verlet integration is: $velocityVerlet.time")
println("Velocity for Velocity Verlet integration is: $velocityVerlet.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 @@ -65,6 +65,8 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod
[import:5-16, lang:"go"](code/golang/verlet.go)
{% sample lang="asm-x64" %}
[import:18-42, lang:"asm-x64"](code/asm-x64/verlet.s)
{% sample lang="kotlin" %}
[import:3-15, lang:"kotlin"](code/kotlin/verlet.kt)
{% 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 @@ -117,6 +119,8 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod
[import:18-30, lang:"go"](code/golang/verlet.go)
{% sample lang="asm-x64" %}
[import:44-71, lang:"asm-x64"](code/asm-x64/verlet.s)
{% sample lang="kotlin" %}
[import:17-30, lang:"kotlin"](code/kotlin/verlet.kt)
{% endmethod %}


Expand Down Expand Up @@ -183,6 +187,8 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod
[import:32-42, lang:"go"](code/golang/verlet.go)
{% sample lang="asm-x64" %}
[import:73-101, lang:"asm-x64"](code/asm-x64/verlet.s)
{% sample lang="kotlin" %}
[import:32-42, lang:"kotlin"](code/kotlin/verlet.kt)
{% 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 @@ -231,6 +237,8 @@ Submitted by P. Mekhail
[import, lang:"go"](code/golang/verlet.go)
{% sample lang="asm-x64" %}
[import, lang:"asm-x64"](code/asm-x64/verlet.s)
{% sample lang="kotlin" %}
[import, lang:"kotlin"](code/kotlin/verlet.kt)
{% endmethod %}


Expand Down