From 8b9c2d12262dcd7142ce49cbe7361b228c5faf91 Mon Sep 17 00:00:00 2001 From: Sklan Date: Thu, 25 Oct 2018 21:03:17 +0530 Subject: [PATCH 1/6] Create verlet.kt --- .../verlet_integration/code/kotlin/verlet.kt | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 contents/verlet_integration/code/kotlin/verlet.kt diff --git a/contents/verlet_integration/code/kotlin/verlet.kt b/contents/verlet_integration/code/kotlin/verlet.kt new file mode 100644 index 000000000..74eff1144 --- /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(_position: Double, acceleration: Double, dt: Double): Double { + var position = _position // Since function parameter are val and can't be modified + var previousPosition = position + var time = 0.0 + + while (position > 0) { + time += dt + val nextPosition = position * 2 - previousPosition + acceleration * dt * dt + previousPosition = position + position = nextPosition + } + return time +} + +fun stormer_verlet(_position: Double, acceleration: Double, dt: Double): VerletValues { + var position = _position + var previousPosition = position + var time = 0.0 + var velocity = 0.0 + while (position > 0) { + time += dt + val nextPosition = position * 2 - previousPosition + acceleration * dt * dt + previousPosition = position + position = nextPosition + velocity += acceleration * dt + } + return VerletValues(time, velocity) +} + +fun velocity_verlet(_position: Double, acceleration: Double, dt: Double): VerletValues { + var position = _position + var time = 0.0 + var velocity = 0.0 + while (position > 0) { + time += dt + position += velocity * dt + 0.5 * acceleration * dt * dt + velocity += acceleration * dt + } + return VerletValues(time, velocity) +} + +fun main(args: Array) { + val verletTime = verlet(5.0, -10.0, 0.01) + println("Time for Verlet integration is: $verletTime") + + val stormerVerlet = stormer_verlet(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 = velocity_verlet(5.0, -10.0, 0.01) + println("Time for velocity Verlet integration is: " + velocityVerlet.time) + println("Velocity for velocity Verlet integration is: " + velocityVerlet.vel) +} From 7165376594fd32ff1daa2361b5e914d05c550900 Mon Sep 17 00:00:00 2001 From: Sklan Date: Thu, 25 Oct 2018 21:06:01 +0530 Subject: [PATCH 2/6] Update verlet_integration.md --- contents/verlet_integration/verlet_integration.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contents/verlet_integration/verlet_integration.md b/contents/verlet_integration/verlet_integration.md index 850a437c2..6324100ad 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 From 3b6c3ca04d28fb1b89f9fd118aefb2c3fb2a3000 Mon Sep 17 00:00:00 2001 From: Sklan Date: Thu, 25 Oct 2018 21:08:35 +0530 Subject: [PATCH 3/6] Update verlet_integration.md --- contents/verlet_integration/verlet_integration.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/contents/verlet_integration/verlet_integration.md b/contents/verlet_integration/verlet_integration.md index 6324100ad..4c198cca7 100644 --- a/contents/verlet_integration/verlet_integration.md +++ b/contents/verlet_integration/verlet_integration.md @@ -119,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 %} @@ -185,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))$$. @@ -233,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 %} From 8594c9b0a33f30a341754c167e8ba793e3940f77 Mon Sep 17 00:00:00 2001 From: Sklan Date: Thu, 25 Oct 2018 21:10:03 +0530 Subject: [PATCH 4/6] Update verlet.kt --- contents/verlet_integration/code/kotlin/verlet.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contents/verlet_integration/code/kotlin/verlet.kt b/contents/verlet_integration/code/kotlin/verlet.kt index 74eff1144..81052261e 100644 --- a/contents/verlet_integration/code/kotlin/verlet.kt +++ b/contents/verlet_integration/code/kotlin/verlet.kt @@ -14,7 +14,7 @@ fun verlet(_position: Double, acceleration: Double, dt: Double): Double { return time } -fun stormer_verlet(_position: Double, acceleration: Double, dt: Double): VerletValues { +fun stormerVerlet(_position: Double, acceleration: Double, dt: Double): VerletValues { var position = _position var previousPosition = position var time = 0.0 @@ -29,7 +29,7 @@ fun stormer_verlet(_position: Double, acceleration: Double, dt: Double): VerletV return VerletValues(time, velocity) } -fun velocity_verlet(_position: Double, acceleration: Double, dt: Double): VerletValues { +fun velocityVerlet(_position: Double, acceleration: Double, dt: Double): VerletValues { var position = _position var time = 0.0 var velocity = 0.0 @@ -45,11 +45,11 @@ fun main(args: Array) { val verletTime = verlet(5.0, -10.0, 0.01) println("Time for Verlet integration is: $verletTime") - val stormerVerlet = stormer_verlet(5.0, -10.0, 0.01) + 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 = velocity_verlet(5.0, -10.0, 0.01) + 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) } From 7a1471b1814e5c527f3da401bb4371d11846c325 Mon Sep 17 00:00:00 2001 From: Sklan Date: Thu, 25 Oct 2018 21:12:33 +0530 Subject: [PATCH 5/6] Update verlet.kt --- .../verlet_integration/code/kotlin/verlet.kt | 52 +++++++++---------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/contents/verlet_integration/code/kotlin/verlet.kt b/contents/verlet_integration/code/kotlin/verlet.kt index 81052261e..725a10cb1 100644 --- a/contents/verlet_integration/code/kotlin/verlet.kt +++ b/contents/verlet_integration/code/kotlin/verlet.kt @@ -1,44 +1,44 @@ data class VerletValues(val time: Double, val vel: Double) -fun verlet(_position: Double, acceleration: Double, dt: Double): Double { - var position = _position // Since function parameter are val and can't be modified - var previousPosition = position +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 (position > 0) { + while (pos > 0) { time += dt - val nextPosition = position * 2 - previousPosition + acceleration * dt * dt - previousPosition = position - position = nextPosition + val nextPos = pos * 2 - prevPos + acc * dt * dt + prevPos = pos + pos = nextPos } return time } -fun stormerVerlet(_position: Double, acceleration: Double, dt: Double): VerletValues { - var position = _position - var previousPosition = position +fun stormerVerlet(_pos: Double, acc: Double, dt: Double): VerletValues { + var pos = _pos + var prevPos = pos var time = 0.0 - var velocity = 0.0 - while (position > 0) { + var vel = 0.0 + while (pos > 0) { time += dt - val nextPosition = position * 2 - previousPosition + acceleration * dt * dt - previousPosition = position - position = nextPosition - velocity += acceleration * dt + val nextPos = pos * 2 - prevPos + acc * dt * dt + prevPos = pos + pos = nextPos + vel += acc * dt } - return VerletValues(time, velocity) + return VerletValues(time, vel) } -fun velocityVerlet(_position: Double, acceleration: Double, dt: Double): VerletValues { - var position = _position +fun velocityVerlet(_pos: Double, acc: Double, dt: Double): VerletValues { + var pos = _pos var time = 0.0 - var velocity = 0.0 - while (position > 0) { + var vel = 0.0 + while (pos > 0) { time += dt - position += velocity * dt + 0.5 * acceleration * dt * dt - velocity += acceleration * dt + pos += vel * dt + 0.5 * acc * dt * dt + vel += acc * dt } - return VerletValues(time, velocity) + return VerletValues(time, vel) } fun main(args: Array) { @@ -50,6 +50,6 @@ fun main(args: Array) { 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) + println("Time for vel Verlet integration is: " + velocityVerlet.time) + println("Velocity for vel Verlet integration is: " + velocityVerlet.vel) } From 0f466bf87445fc43b6197ba7538cf5e92d7db5c1 Mon Sep 17 00:00:00 2001 From: Sklan Date: Sun, 28 Oct 2018 11:45:11 +0530 Subject: [PATCH 6/6] Making requested changes --- contents/verlet_integration/code/kotlin/verlet.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contents/verlet_integration/code/kotlin/verlet.kt b/contents/verlet_integration/code/kotlin/verlet.kt index 725a10cb1..c2d41b3ff 100644 --- a/contents/verlet_integration/code/kotlin/verlet.kt +++ b/contents/verlet_integration/code/kotlin/verlet.kt @@ -46,10 +46,10 @@ fun main(args: Array) { 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) + 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 vel Verlet integration is: " + velocityVerlet.time) - println("Velocity for vel Verlet integration is: " + velocityVerlet.vel) + println("Time for Velocity Verlet integration is: $velocityVerlet.time") + println("Velocity for Velocity Verlet integration is: $velocityVerlet.vel") }