diff --git a/contents/verlet_integration/code/kotlin/verlet.kt b/contents/verlet_integration/code/kotlin/verlet.kt new file mode 100644 index 000000000..c2d41b3ff --- /dev/null +++ b/contents/verlet_integration/code/kotlin/verlet.kt @@ -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) { + 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") +} diff --git a/contents/verlet_integration/verlet_integration.md b/contents/verlet_integration/verlet_integration.md index 850a437c2..4c198cca7 100644 --- a/contents/verlet_integration/verlet_integration.md +++ b/contents/verlet_integration/verlet_integration.md @@ -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 @@ -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 %} @@ -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))$$. @@ -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 %}