Skip to content

Commit b8d4679

Browse files
stormoficejiegilletleios
authored
Verlet Integration: Output standardization (#855)
* Standardize julia output * Standardize kotlin output This also fixes a previous bug, which caused the time and velocity values to not get printed correctly. * Standardized c output * Standardized cpp output * Standardized lisp output * Standardized fortran output I was not able to prevent the preceding whitespaces, but they can just be trimmed. * Standardized go output * Standardized java output * Standardize javascript output * Standardize nim output * Standardize python output * Standardize ruby output As the original implementation only returned the time and not the velocity, the code needed to be adjusted a bit. Now it returns the two values as an array which gets deconstructed and printed. * Standardize rust output * Standardize swift output * Standardized haskell output * Standardized haskell output (no quote marks) * attempt at fix for asm Co-authored-by: Jérémie Gillet <jie.gillet@gmail.com> Co-authored-by: James Schloss <jrs.schloss@gmail.com>
1 parent cbe4b61 commit b8d4679

File tree

16 files changed

+167
-93
lines changed

16 files changed

+167
-93
lines changed

contents/verlet_integration/code/asm-x64/verlet.s

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
zero: .double 0.0
55
two: .double 2.0
66
half: .double 0.5
7-
verlet_fmt: .string "Time for Verlet integration is: %lf\n"
8-
stormer_fmt: .string "Time and Velocity for Stormer Verlet Integration is: %lf, %lf\n"
9-
velocity_fmt: .string "Time and Velocity for Velocity Verlet Integration is: %lf, %lf\n"
7+
verlet_fmt: .string "[#] Time for Verlet integration is:\n%lf\n"
8+
stormer_fmt: .string "[#] Time for Stormer Verlet Integration is:\n%lf\n[#] Velocity for Stormer Verlet Integration is:\n%lf\n"
9+
velocity_fmt: .string "[#] Time for Velocity Verlet Integration is:\n%lf\n[#] Velocity for Velocity Verlet Integration is:\n%lf\n"
1010
pos: .double 5.0
1111
acc: .double -10.0
1212
dt: .double 0.01

contents/verlet_integration/code/c++/verlet.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,19 @@ int main() {
6464
// each of these functions.
6565

6666
double time = verlet(5.0, -10, 0.01);
67-
std::cout << "Time for Verlet integration is: " \
67+
std::cout << "[#] Time for Verlet integration is:\n" \
6868
<< time << std::endl;
6969

7070
timestep timestep_sv = stormer_verlet(5.0, -10, 0.01);
71-
std::cout << "Time for Stormer Verlet integration is: " \
71+
std::cout << "[#] Time for Stormer Verlet integration is:\n" \
7272
<< timestep_sv.time << std::endl;
73-
std::cout << "Velocity for Stormer Verlet integration is: " \
73+
std::cout << "[#] Velocity for Stormer Verlet integration is:\n" \
7474
<< timestep_sv.vel << std::endl;
7575

7676
timestep timestep_vv = velocity_verlet(5.0, -10, 0.01);
77-
std::cout << "Time for velocity Verlet integration is: " \
77+
std::cout << "[#] Time for velocity Verlet integration is:\n" \
7878
<< timestep_vv.time << std::endl;
79-
std::cout << "Velocity for velocity Verlet integration is: " \
79+
std::cout << "[#] Velocity for velocity Verlet integration is:\n" \
8080
<< timestep_vv.vel << std::endl;
8181

8282
return 0;

contents/verlet_integration/code/c/verlet.c

+10-6
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,20 @@ int main() {
4646
double time, vel;
4747

4848
verlet(&time, 5.0, -10, 0.01);
49-
printf("Time for Verlet integration is: %lf\n",
50-
time);
49+
printf("[#] Time for Verlet integration is:\n");
50+
printf("%lf\n", time);
5151

5252
stormer_verlet(&time, &vel, 5.0, -10, 0.01);
53-
printf("Time and velocity for Stormer Verlet integration is: %lf, %lf\n",
54-
time, vel);
53+
printf("[#] Time for Stormer Verlet integration is:\n");
54+
printf("%lf\n", time);
55+
printf("[#] Velocity for Stormer Verlet integration is:\n");
56+
printf("%lf\n", vel);
5557

5658
velocity_verlet(&time, &vel, 5.0, -10, 0.01);
57-
printf("Time and velocity for velocity Verlet integration is: %lf, %lf\n",
58-
time, vel);
59+
printf("[#] Time for velocity Verlet integration is:\n");
60+
printf("%lf\n", time);
61+
printf("[#] Velocity for Stormer Verlet integration is:\n");
62+
printf("%lf\n", vel);
5963

6064
return 0;
6165
}

contents/verlet_integration/code/clisp/verlet.lisp

+10-5
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,17 @@
3434
while (> p 0)
3535
finally (return (list time vel))))
3636

37-
(format T "Time for Verlet integration: ~d~%" (verlet 5 -10 0.01))
37+
(format T "[#] Time for Verlet integration:~%")
38+
(format T "~d~%" (verlet 5 -10 0.01))
3839

3940
(defvar stormer-verlet-result (stormer-verlet 5 -10 0.01))
40-
(format T "Time for Stormer Verlet integration is: ~d~%" (first stormer-verlet-result))
41-
(format T "Velocity for Stormer Verlet integration is: ~d~%" (second stormer-verlet-result))
41+
(format T "[#] Time for Stormer Verlet integration is:~%")
42+
(format T "~d~%" (first stormer-verlet-result))
43+
(format T "[#] Velocity for Stormer Verlet integration is:~%")
44+
(format T "~d~%" (second stormer-verlet-result))
4245

4346
(defvar velocity-verlet-result (velocity-verlet 5 -10 0.01))
44-
(format T "Time for velocity Verlet integration is: ~d~%" (first velocity-verlet-result))
45-
(format T "Velocity for velocity Verlet integration is: ~d~%" (second velocity-verlet-result))
47+
(format T "[#] Time for velocity Verlet integration is:~%")
48+
(format T "~d~%" (first velocity-verlet-result))
49+
(format T "[#] Velocity for velocity Verlet integration is:~%")
50+
(format T "~d~%" (second velocity-verlet-result))

contents/verlet_integration/code/fortran/verlet.f90

+13-4
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,27 @@ SUBROUTINE velocity_verlet(pos, acc, dt, time, vel)
9191
! Verlet
9292
CALL verlet(pos, acc, dt, time)
9393

94-
WRITE(*,*) 'Time for Verlet integration: ', time
94+
WRITE(*,*) '[#] Time for Verlet integration:'
95+
WRITE(*,*) time
9596

9697
! stormer Verlet
9798
pos = 5d0
9899
CALL stormer_verlet(pos, acc, dt, time, vel)
99100

100-
WRITE(*,*) 'Time for Stormer-Verlet integration: ', time
101+
WRITE(*,*) '[#] Time for Stormer Verlet integration:'
102+
WRITE(*,*) time
103+
WRITE(*,*) '[#] Velocity for Stormer Verlet integration:'
104+
WRITE(*,*) vel
105+
106+
101107

102108
! Velocity Verlet
103109
pos = 5d0
104110
CALL velocity_verlet(pos, acc, dt, time, vel)
105111

106-
WRITE(*,*) 'Time for Velocity-Verlet integration: ', time
107-
112+
WRITE(*,*) '[#] Time for velocity Verlet integration:'
113+
WRITE(*,*) time
114+
WRITE(*,*) '[#] Velocity for velocity Verlet integration:'
115+
WRITE(*,*) vel
116+
108117
END PROGRAM verlet_integration

contents/verlet_integration/code/golang/verlet.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@ func velocityVerlet(pos, acc, dt float64) (time, vel float64) {
4343

4444
func main() {
4545
time := verlet(5., -10., .01)
46-
fmt.Println("Verlet")
47-
fmt.Println("Time:", time)
48-
fmt.Println()
46+
fmt.Println("[#] Time for Verlet integration is:")
47+
fmt.Println(time)
4948

5049
time, vel := stormerVerlet(5., -10., .01)
51-
fmt.Println("Stormer-Verlet")
52-
fmt.Println("Time:", time)
53-
fmt.Println("Velocity:", vel)
54-
fmt.Println()
55-
50+
fmt.Println("[#] Time for Stormer Verlet integration is:")
51+
fmt.Println(time)
52+
fmt.Println("[#] Velocity for Stormer Verlet integration is:")
53+
fmt.Println(vel)
54+
5655
time, vel = velocityVerlet(5., -10., .01)
57-
fmt.Println("Velocity Verlet")
58-
fmt.Println("Time:", time)
59-
fmt.Println("Velocity:", vel)
56+
fmt.Println("[#] Time for velocity Verlet integration is:")
57+
fmt.Println(time)
58+
fmt.Println("[#] Velocity for velocity Verlet integration is:")
59+
fmt.Println(vel)
6060
}

contents/verlet_integration/code/haskell/verlet.hs

+14-4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,17 @@ main = do
4848
dt = 0.001
4949
freefall _ = -10
5050
aboveGround (x, _, _, _) = x > 0
51-
integrate m = last $ takeWhile aboveGround $ trajectory m freefall dt p0
52-
print $ integrate verlet
53-
print $ integrate stormerVerlet
54-
print $ integrate velocityVerlet
51+
timeVelocity m =
52+
let (_, v, _, t) = last $ takeWhile aboveGround $ trajectory m freefall dt p0
53+
in (show t, show v)
54+
55+
putStrLn "[#] Time for Verlet integration is:"
56+
putStrLn $ fst $ timeVelocity verlet
57+
putStrLn "[#] Time for Stormer Verlet integration is:"
58+
putStrLn $ fst $ timeVelocity stormerVerlet
59+
putStrLn "[#] Velocity for Stormer Verlet integration is:"
60+
putStrLn $ snd $ timeVelocity stormerVerlet
61+
putStrLn "[#] Time for velocity Verlet integration is:"
62+
putStrLn $ fst $ timeVelocity velocityVerlet
63+
putStrLn "[#] Velocity for velocity Verlet integration is:"
64+
putStrLn $ snd $ timeVelocity velocityVerlet

contents/verlet_integration/code/java/Verlet.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,20 @@ static VerletValues velocity_verlet(double pos, double acc, double dt) {
6565
public static void main(String[] args) {
6666

6767
double verletTime = verlet(5.0, -10, 0.01);
68-
System.out.println("Time for Verlet integration is: " + verletTime);
68+
System.out.println("[#] Time for Verlet integration is:");
69+
System.out.println(verletTime);
6970

7071
VerletValues stormerVerlet = stormer_verlet(5.0, -10, 0.01);
71-
System.out.println("Time for Stormer Verlet integration is: " + stormerVerlet.time);
72-
System.out.println("Velocity for Stormer Verlet integration is: " + stormerVerlet.vel);
72+
System.out.println("[#] Time for Stormer Verlet integration is:");
73+
System.out.println(stormerVerlet.time);
74+
System.out.println("[#] Velocity for Stormer Verlet integration is:");
75+
System.out.println(stormerVerlet.vel);
7376

7477
VerletValues velocityVerlet = velocity_verlet(5.0, -10, 0.01);
75-
System.out.println("Time for velocity Verlet integration is: " + velocityVerlet.time);
76-
System.out.println("Velocity for velocity Verlet integration is: " + velocityVerlet.vel);
78+
System.out.println("[#] Time for velocity Verlet integration is:");
79+
System.out.println(velocityVerlet.time);
80+
System.out.println("[#] Velocity for velocity Verlet integration is:");
81+
System.out.println(velocityVerlet.vel);
82+
7783
}
7884
}

contents/verlet_integration/code/javascript/verlet.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,17 @@ function velocityVerlet(pos, acc, dt) {
4545
}
4646

4747
const time = verlet(5, -10, 0.01);
48-
console.log(`Time for Verlet integration is: ${time}\n`);
48+
console.log(`[#] Time for Verlet integration is:`);
49+
console.log(`${time}`);
4950

5051
const stormer = stormerVerlet(5, -10, 0.01);
51-
console.log(`Time for Stormer Verlet integration is: ${stormer.time}`);
52-
console.log(`Velocity for Stormer Verlet integration is: ${stormer.vel}\n`);
52+
console.log(`[#] Time for Stormer Verlet integration is:`);
53+
console.log(`${stormer.time}`);
54+
console.log(`[#] Velocity for Stormer Verlet integration is:`);
55+
console.log(`${stormer.vel}`);
5356

5457
const velocity = velocityVerlet(5, -10, 0.01);
55-
console.log(`Time for Velocity Verlet integration is: ${velocity.time}`);
56-
console.log(`Velocity for Velocity Verlet integration is: ${velocity.vel}\n`);
58+
console.log(`[#] Time for velocity Verlet integration is:`);
59+
console.log(`${velocity.time}`);
60+
console.log(`[#] Velocity for velocity Verlet integration is:`);
61+
console.log(`${velocity.vel}`);

contents/verlet_integration/code/julia/verlet.jl

+12-6
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,21 @@ end
4646

4747
function main()
4848
time = verlet(5.0, -10.0, 0.01);
49-
println("Time for Verlet integration is: $(time)\n")
49+
println("[#] Time for Verlet integration is:")
50+
println("$(time)")
5051

5152
time, vel = stormer_verlet(5.0, -10.0, 0.01);
52-
println("Time for Stormer Verlet integration is: $(time)")
53-
println("Velocity for Stormer Verlet integration is: $(vel)\n")
54-
53+
println("[#] Time for Stormer Verlet integration is:")
54+
println("$(time)")
55+
println("[#] Velocity for Stormer Verlet integration is:")
56+
println("$(vel)")
57+
5558
time, vel = velocity_verlet(5.0, -10.0, 0.01);
56-
println("Time for velocity Verlet integration is: $(time)")
57-
println("Velocity for velocity Verlet integration is: $(vel)\n")
59+
println("[#] Time for velocity Verlet integration is:")
60+
println("$(time)")
61+
println("[#] Velocity for velocity Verlet integration is:")
62+
println("$(vel)")
63+
5864
end
5965

6066
main()

contents/verlet_integration/code/kotlin/verlet.kt

+10-5
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,18 @@ fun velocityVerlet(_pos: Double, acc: Double, dt: Double): VerletValues {
4343

4444
fun main(args: Array<String>) {
4545
val verletTime = verlet(5.0, -10.0, 0.01)
46-
println("Time for Verlet integration is: $verletTime")
46+
println("[#] Time for Verlet integration is:")
47+
println("$verletTime")
4748

4849
val stormerVerlet = stormerVerlet(5.0, -10.0, 0.01)
49-
println("Time for Stormer Verlet integration is: $stormerVerlet.time")
50-
println("Velocity for Stormer Verlet integration is: $stormerVerlet.vel")
50+
println("[#] Time for Stormer Verlet integration is:")
51+
println("${stormerVerlet.time}")
52+
println("[#] Velocity for Stormer Verlet integration is:")
53+
println("${stormerVerlet.vel}")
5154

5255
val velocityVerlet = velocityVerlet(5.0, -10.0, 0.01)
53-
println("Time for Velocity Verlet integration is: $velocityVerlet.time")
54-
println("Velocity for Velocity Verlet integration is: $velocityVerlet.vel")
56+
println("[#] Time for Velocity Verlet integration is:")
57+
println("${velocityVerlet.time}")
58+
println("[#] Velocity for Velocity Verlet integration is:")
59+
println("${velocityVerlet.vel}")
5560
}

contents/verlet_integration/code/nim/verlet.nim

+10-5
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,17 @@ func velocityVerlet(pos_in, acc, dt: float): (float, float) =
4646

4747
when isMainModule:
4848
let timeV = verlet(5.0, -10.0, 0.01)
49-
echo "Time for Verlet integration is: ", timeV
49+
echo "[#] Time for Verlet integration is:"
50+
echo timeV
5051

5152
let (timeSV, velSV) = stormerVerlet(5.0, -10.0, 0.01)
52-
echo "Time for Stormer Verlet integration is: ", timeSV
53-
echo "Velocity for Stormer Verlet integration is: ", velSV
53+
echo "[#] Time for Stormer Verlet integration is:"
54+
echo timeSV
55+
echo "[#] Velocity for Stormer Verlet integration is:"
56+
echo velSV
5457

5558
let (timeVV, velVV) = velocityVerlet(5.0, -10.0, 0.01)
56-
echo "Time for velocity Verlet integration is: ", timeVV
57-
echo "Velocity for velocity Verlet integration is: ", velVV
59+
echo "[#] Time for velocity Verlet integration is:"
60+
echo timeVV
61+
echo "[#] Velocity for velocity Verlet integration is:"
62+
echo velVV

contents/verlet_integration/code/python/verlet.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@ def velocity_verlet(pos, acc, dt):
3535

3636
def main():
3737
time = verlet(5, -10, 0.01)
38-
print("Verlet")
39-
print("Time: {:.10f}".format(time))
40-
print()
38+
print("[#] Time for Verlet integration is:")
39+
print("{:.10f}".format(time))
4140

4241
time, vel = stormer_verlet(5, -10, 0.01)
43-
print("Stormer-Verlet")
44-
print("Time: {:.10f}".format(time))
45-
print("Velocity: {:.10f}".format(vel))
46-
print()
42+
print("[#] Time for Stormer Verlet integration is:")
43+
print("{:.10f}".format(time))
44+
print("[#] Velocity for Stormer Verlet integration is:")
45+
print("{:.10f}".format(vel))
4746

4847
time, vel = velocity_verlet(5, -10, 0.01)
49-
print("Velocity Verlet")
50-
print("Time: {:.10f}".format(time))
51-
print("Velocity: {:.10f}".format(vel))
52-
print()
48+
print("[#] Time for velocity Verlet integration is:")
49+
print("{:.10f}".format(time))
50+
print("[#] Velocity for velocity Verlet integration is:")
51+
print("{:.10f}".format(vel))
52+
5353

5454
if __name__ == '__main__':
5555
main()

contents/verlet_integration/code/ruby/verlet.rb

+15-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def stormer_verlet(pos, acc, dt)
2727
vel += acc*dt
2828
end
2929

30-
return time
30+
return time, vel
3131

3232
end
3333

@@ -41,10 +41,21 @@ def velocity_verlet(pos, acc, dt)
4141
vel += acc*dt
4242
end
4343

44-
return time
44+
return time, vel
4545

4646
end
4747

48+
puts "[#] Time for Verlet integration is:"
4849
p verlet(5.0, -10, 0.01)
49-
p stormer_verlet(5.0, -10, 0.01)
50-
p velocity_verlet(5.0, -10, 0.01)
50+
51+
time, vel = stormer_verlet(5.0, -10, 0.01)
52+
puts "[#] Time for Stormer Verlet integration is:"
53+
p time
54+
puts "[#] Velocity for Stormer Verlet integration is:"
55+
p vel
56+
57+
time, vel = velocity_verlet(5.0, -10, 0.01)
58+
puts "[#] Time for velocity Verlet integration is:"
59+
p time
60+
puts "[#] Velocity for velocity Verlet integration is:"
61+
p vel

contents/verlet_integration/code/rust/verlet.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,16 @@ fn main() {
4949
let (time_sv, vel_sv) = stormer_verlet(5.0, -10.0, 0.01);
5050
let (time_vv, vel_vv) = velocity_verlet(5.0, -10.0, 0.01);
5151

52-
println!("Time for original Verlet integration: {}", time_v);
53-
println!(
54-
"Time and velocity for Stormer Verlet integration: {}, {}",
55-
time_sv, vel_sv
56-
);
57-
println!(
58-
"Time and velocity for velocity Verlet integration: {}, {}",
59-
time_vv, vel_vv
60-
);
52+
println!("[#] Time for Verlet integration is:");
53+
println!("{}", time_v);
54+
55+
println!("[#] Time for Stormer Verlet integration is:");
56+
println!("{}", time_sv);
57+
println!("[#] Velocity for Stormer Verlet integration is:");
58+
println!("{}", vel_sv);
59+
60+
println!("[#] Time for velocity Verlet integration is:");
61+
println!("{}", time_vv);
62+
println!("[#] Velocity for velocity Verlet integration is:");
63+
println!("{}", vel_vv);
6164
}

0 commit comments

Comments
 (0)