forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverlet.kt
55 lines (48 loc) · 1.58 KB
/
verlet.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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")
}