From b8d4679df76141d9a2a129af9264701b58d33565 Mon Sep 17 00:00:00 2001 From: stormofice <58337328+stormofice@users.noreply.github.com> Date: Sat, 4 Sep 2021 04:42:51 +0200 Subject: [PATCH] Verlet Integration: Output standardization (#855) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 Co-authored-by: James Schloss --- .../verlet_integration/code/asm-x64/verlet.s | 6 ++--- .../verlet_integration/code/c++/verlet.cpp | 10 ++++----- contents/verlet_integration/code/c/verlet.c | 16 +++++++++----- .../verlet_integration/code/clisp/verlet.lisp | 15 ++++++++----- .../code/fortran/verlet.f90 | 17 ++++++++++---- .../verlet_integration/code/golang/verlet.go | 22 +++++++++---------- .../verlet_integration/code/haskell/verlet.hs | 18 +++++++++++---- .../verlet_integration/code/java/Verlet.java | 16 +++++++++----- .../code/javascript/verlet.js | 15 ++++++++----- .../verlet_integration/code/julia/verlet.jl | 18 ++++++++++----- .../verlet_integration/code/kotlin/verlet.kt | 15 ++++++++----- .../verlet_integration/code/nim/verlet.nim | 15 ++++++++----- .../verlet_integration/code/python/verlet.py | 22 +++++++++---------- .../verlet_integration/code/ruby/verlet.rb | 19 ++++++++++++---- .../verlet_integration/code/rust/verlet.rs | 21 ++++++++++-------- .../code/swift/verlet.swift | 15 ++++++++----- 16 files changed, 167 insertions(+), 93 deletions(-) diff --git a/contents/verlet_integration/code/asm-x64/verlet.s b/contents/verlet_integration/code/asm-x64/verlet.s index ed2521f78..d9617fe33 100644 --- a/contents/verlet_integration/code/asm-x64/verlet.s +++ b/contents/verlet_integration/code/asm-x64/verlet.s @@ -4,9 +4,9 @@ zero: .double 0.0 two: .double 2.0 half: .double 0.5 - verlet_fmt: .string "Time for Verlet integration is: %lf\n" - stormer_fmt: .string "Time and Velocity for Stormer Verlet Integration is: %lf, %lf\n" - velocity_fmt: .string "Time and Velocity for Velocity Verlet Integration is: %lf, %lf\n" + verlet_fmt: .string "[#] Time for Verlet integration is:\n%lf\n" + stormer_fmt: .string "[#] Time for Stormer Verlet Integration is:\n%lf\n[#] Velocity for Stormer Verlet Integration is:\n%lf\n" + velocity_fmt: .string "[#] Time for Velocity Verlet Integration is:\n%lf\n[#] Velocity for Velocity Verlet Integration is:\n%lf\n" pos: .double 5.0 acc: .double -10.0 dt: .double 0.01 diff --git a/contents/verlet_integration/code/c++/verlet.cpp b/contents/verlet_integration/code/c++/verlet.cpp index 473f92ee9..946ddc618 100644 --- a/contents/verlet_integration/code/c++/verlet.cpp +++ b/contents/verlet_integration/code/c++/verlet.cpp @@ -64,19 +64,19 @@ int main() { // each of these functions. double time = verlet(5.0, -10, 0.01); - std::cout << "Time for Verlet integration is: " \ + std::cout << "[#] Time for Verlet integration is:\n" \ << time << std::endl; timestep timestep_sv = stormer_verlet(5.0, -10, 0.01); - std::cout << "Time for Stormer Verlet integration is: " \ + std::cout << "[#] Time for Stormer Verlet integration is:\n" \ << timestep_sv.time << std::endl; - std::cout << "Velocity for Stormer Verlet integration is: " \ + std::cout << "[#] Velocity for Stormer Verlet integration is:\n" \ << timestep_sv.vel << std::endl; timestep timestep_vv = velocity_verlet(5.0, -10, 0.01); - std::cout << "Time for velocity Verlet integration is: " \ + std::cout << "[#] Time for velocity Verlet integration is:\n" \ << timestep_vv.time << std::endl; - std::cout << "Velocity for velocity Verlet integration is: " \ + std::cout << "[#] Velocity for velocity Verlet integration is:\n" \ << timestep_vv.vel << std::endl; return 0; diff --git a/contents/verlet_integration/code/c/verlet.c b/contents/verlet_integration/code/c/verlet.c index c56a3cc5b..c42254974 100644 --- a/contents/verlet_integration/code/c/verlet.c +++ b/contents/verlet_integration/code/c/verlet.c @@ -46,16 +46,20 @@ int main() { double time, vel; verlet(&time, 5.0, -10, 0.01); - printf("Time for Verlet integration is: %lf\n", - time); + printf("[#] Time for Verlet integration is:\n"); + printf("%lf\n", time); stormer_verlet(&time, &vel, 5.0, -10, 0.01); - printf("Time and velocity for Stormer Verlet integration is: %lf, %lf\n", - time, vel); + printf("[#] Time for Stormer Verlet integration is:\n"); + printf("%lf\n", time); + printf("[#] Velocity for Stormer Verlet integration is:\n"); + printf("%lf\n", vel); velocity_verlet(&time, &vel, 5.0, -10, 0.01); - printf("Time and velocity for velocity Verlet integration is: %lf, %lf\n", - time, vel); + printf("[#] Time for velocity Verlet integration is:\n"); + printf("%lf\n", time); + printf("[#] Velocity for Stormer Verlet integration is:\n"); + printf("%lf\n", vel); return 0; } diff --git a/contents/verlet_integration/code/clisp/verlet.lisp b/contents/verlet_integration/code/clisp/verlet.lisp index 726b1f1c6..f08f2a7e6 100644 --- a/contents/verlet_integration/code/clisp/verlet.lisp +++ b/contents/verlet_integration/code/clisp/verlet.lisp @@ -34,12 +34,17 @@ while (> p 0) finally (return (list time vel)))) -(format T "Time for Verlet integration: ~d~%" (verlet 5 -10 0.01)) +(format T "[#] Time for Verlet integration:~%") +(format T "~d~%" (verlet 5 -10 0.01)) (defvar stormer-verlet-result (stormer-verlet 5 -10 0.01)) -(format T "Time for Stormer Verlet integration is: ~d~%" (first stormer-verlet-result)) -(format T "Velocity for Stormer Verlet integration is: ~d~%" (second stormer-verlet-result)) +(format T "[#] Time for Stormer Verlet integration is:~%") +(format T "~d~%" (first stormer-verlet-result)) +(format T "[#] Velocity for Stormer Verlet integration is:~%") +(format T "~d~%" (second stormer-verlet-result)) (defvar velocity-verlet-result (velocity-verlet 5 -10 0.01)) -(format T "Time for velocity Verlet integration is: ~d~%" (first velocity-verlet-result)) -(format T "Velocity for velocity Verlet integration is: ~d~%" (second velocity-verlet-result)) +(format T "[#] Time for velocity Verlet integration is:~%") +(format T "~d~%" (first velocity-verlet-result)) +(format T "[#] Velocity for velocity Verlet integration is:~%") +(format T "~d~%" (second velocity-verlet-result)) \ No newline at end of file diff --git a/contents/verlet_integration/code/fortran/verlet.f90 b/contents/verlet_integration/code/fortran/verlet.f90 index 417fc77b7..6999df1e5 100644 --- a/contents/verlet_integration/code/fortran/verlet.f90 +++ b/contents/verlet_integration/code/fortran/verlet.f90 @@ -91,18 +91,27 @@ SUBROUTINE velocity_verlet(pos, acc, dt, time, vel) ! Verlet CALL verlet(pos, acc, dt, time) - WRITE(*,*) 'Time for Verlet integration: ', time + WRITE(*,*) '[#] Time for Verlet integration:' + WRITE(*,*) time ! stormer Verlet pos = 5d0 CALL stormer_verlet(pos, acc, dt, time, vel) - WRITE(*,*) 'Time for Stormer-Verlet integration: ', time + WRITE(*,*) '[#] Time for Stormer Verlet integration:' + WRITE(*,*) time + WRITE(*,*) '[#] Velocity for Stormer Verlet integration:' + WRITE(*,*) vel + + ! Velocity Verlet pos = 5d0 CALL velocity_verlet(pos, acc, dt, time, vel) - WRITE(*,*) 'Time for Velocity-Verlet integration: ', time - + WRITE(*,*) '[#] Time for velocity Verlet integration:' + WRITE(*,*) time + WRITE(*,*) '[#] Velocity for velocity Verlet integration:' + WRITE(*,*) vel + END PROGRAM verlet_integration diff --git a/contents/verlet_integration/code/golang/verlet.go b/contents/verlet_integration/code/golang/verlet.go index 778521da4..a7cd1c86b 100644 --- a/contents/verlet_integration/code/golang/verlet.go +++ b/contents/verlet_integration/code/golang/verlet.go @@ -43,18 +43,18 @@ func velocityVerlet(pos, acc, dt float64) (time, vel float64) { func main() { time := verlet(5., -10., .01) - fmt.Println("Verlet") - fmt.Println("Time:", time) - fmt.Println() + fmt.Println("[#] Time for Verlet integration is:") + fmt.Println(time) time, vel := stormerVerlet(5., -10., .01) - fmt.Println("Stormer-Verlet") - fmt.Println("Time:", time) - fmt.Println("Velocity:", vel) - fmt.Println() - + fmt.Println("[#] Time for Stormer Verlet integration is:") + fmt.Println(time) + fmt.Println("[#] Velocity for Stormer Verlet integration is:") + fmt.Println(vel) + time, vel = velocityVerlet(5., -10., .01) - fmt.Println("Velocity Verlet") - fmt.Println("Time:", time) - fmt.Println("Velocity:", vel) + fmt.Println("[#] Time for velocity Verlet integration is:") + fmt.Println(time) + fmt.Println("[#] Velocity for velocity Verlet integration is:") + fmt.Println(vel) } diff --git a/contents/verlet_integration/code/haskell/verlet.hs b/contents/verlet_integration/code/haskell/verlet.hs index df82004e6..675c7f39b 100644 --- a/contents/verlet_integration/code/haskell/verlet.hs +++ b/contents/verlet_integration/code/haskell/verlet.hs @@ -48,7 +48,17 @@ main = do dt = 0.001 freefall _ = -10 aboveGround (x, _, _, _) = x > 0 - integrate m = last $ takeWhile aboveGround $ trajectory m freefall dt p0 - print $ integrate verlet - print $ integrate stormerVerlet - print $ integrate velocityVerlet + timeVelocity m = + let (_, v, _, t) = last $ takeWhile aboveGround $ trajectory m freefall dt p0 + in (show t, show v) + + putStrLn "[#] Time for Verlet integration is:" + putStrLn $ fst $ timeVelocity verlet + putStrLn "[#] Time for Stormer Verlet integration is:" + putStrLn $ fst $ timeVelocity stormerVerlet + putStrLn "[#] Velocity for Stormer Verlet integration is:" + putStrLn $ snd $ timeVelocity stormerVerlet + putStrLn "[#] Time for velocity Verlet integration is:" + putStrLn $ fst $ timeVelocity velocityVerlet + putStrLn "[#] Velocity for velocity Verlet integration is:" + putStrLn $ snd $ timeVelocity velocityVerlet diff --git a/contents/verlet_integration/code/java/Verlet.java b/contents/verlet_integration/code/java/Verlet.java index a7bb94447..35387cf8b 100644 --- a/contents/verlet_integration/code/java/Verlet.java +++ b/contents/verlet_integration/code/java/Verlet.java @@ -65,14 +65,20 @@ static VerletValues velocity_verlet(double pos, double acc, double dt) { public static void main(String[] args) { double verletTime = verlet(5.0, -10, 0.01); - System.out.println("Time for Verlet integration is: " + verletTime); + System.out.println("[#] Time for Verlet integration is:"); + System.out.println(verletTime); VerletValues stormerVerlet = stormer_verlet(5.0, -10, 0.01); - System.out.println("Time for Stormer Verlet integration is: " + stormerVerlet.time); - System.out.println("Velocity for Stormer Verlet integration is: " + stormerVerlet.vel); + System.out.println("[#] Time for Stormer Verlet integration is:"); + System.out.println(stormerVerlet.time); + System.out.println("[#] Velocity for Stormer Verlet integration is:"); + System.out.println(stormerVerlet.vel); VerletValues velocityVerlet = velocity_verlet(5.0, -10, 0.01); - System.out.println("Time for velocity Verlet integration is: " + velocityVerlet.time); - System.out.println("Velocity for velocity Verlet integration is: " + velocityVerlet.vel); + System.out.println("[#] Time for velocity Verlet integration is:"); + System.out.println(velocityVerlet.time); + System.out.println("[#] Velocity for velocity Verlet integration is:"); + System.out.println(velocityVerlet.vel); + } } diff --git a/contents/verlet_integration/code/javascript/verlet.js b/contents/verlet_integration/code/javascript/verlet.js index 013a7fa3f..7ea09e187 100644 --- a/contents/verlet_integration/code/javascript/verlet.js +++ b/contents/verlet_integration/code/javascript/verlet.js @@ -45,12 +45,17 @@ function velocityVerlet(pos, acc, dt) { } const time = verlet(5, -10, 0.01); -console.log(`Time for Verlet integration is: ${time}\n`); +console.log(`[#] Time for Verlet integration is:`); +console.log(`${time}`); const stormer = stormerVerlet(5, -10, 0.01); -console.log(`Time for Stormer Verlet integration is: ${stormer.time}`); -console.log(`Velocity for Stormer Verlet integration is: ${stormer.vel}\n`); +console.log(`[#] Time for Stormer Verlet integration is:`); +console.log(`${stormer.time}`); +console.log(`[#] Velocity for Stormer Verlet integration is:`); +console.log(`${stormer.vel}`); const velocity = velocityVerlet(5, -10, 0.01); -console.log(`Time for Velocity Verlet integration is: ${velocity.time}`); -console.log(`Velocity for Velocity Verlet integration is: ${velocity.vel}\n`); +console.log(`[#] Time for velocity Verlet integration is:`); +console.log(`${velocity.time}`); +console.log(`[#] Velocity for velocity Verlet integration is:`); +console.log(`${velocity.vel}`); diff --git a/contents/verlet_integration/code/julia/verlet.jl b/contents/verlet_integration/code/julia/verlet.jl index b55b85a9e..b9edcea98 100644 --- a/contents/verlet_integration/code/julia/verlet.jl +++ b/contents/verlet_integration/code/julia/verlet.jl @@ -46,15 +46,21 @@ end function main() time = verlet(5.0, -10.0, 0.01); - println("Time for Verlet integration is: $(time)\n") + println("[#] Time for Verlet integration is:") + println("$(time)") time, vel = stormer_verlet(5.0, -10.0, 0.01); - println("Time for Stormer Verlet integration is: $(time)") - println("Velocity for Stormer Verlet integration is: $(vel)\n") - + println("[#] Time for Stormer Verlet integration is:") + println("$(time)") + println("[#] Velocity for Stormer Verlet integration is:") + println("$(vel)") + time, vel = velocity_verlet(5.0, -10.0, 0.01); - println("Time for velocity Verlet integration is: $(time)") - println("Velocity for velocity Verlet integration is: $(vel)\n") + println("[#] Time for velocity Verlet integration is:") + println("$(time)") + println("[#] Velocity for velocity Verlet integration is:") + println("$(vel)") + end main() diff --git a/contents/verlet_integration/code/kotlin/verlet.kt b/contents/verlet_integration/code/kotlin/verlet.kt index c2d41b3ff..79bee7b6a 100644 --- a/contents/verlet_integration/code/kotlin/verlet.kt +++ b/contents/verlet_integration/code/kotlin/verlet.kt @@ -43,13 +43,18 @@ fun velocityVerlet(_pos: Double, acc: Double, dt: Double): VerletValues { fun main(args: Array) { val verletTime = verlet(5.0, -10.0, 0.01) - println("Time for Verlet integration is: $verletTime") + println("[#] Time for Verlet integration is:") + println("$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:") + println("${stormerVerlet.time}") + println("[#] Velocity for Stormer Verlet integration is:") + println("${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 Velocity Verlet integration is:") + println("${velocityVerlet.time}") + println("[#] Velocity for Velocity Verlet integration is:") + println("${velocityVerlet.vel}") } diff --git a/contents/verlet_integration/code/nim/verlet.nim b/contents/verlet_integration/code/nim/verlet.nim index d88a2e586..2e92b57c4 100644 --- a/contents/verlet_integration/code/nim/verlet.nim +++ b/contents/verlet_integration/code/nim/verlet.nim @@ -46,12 +46,17 @@ func velocityVerlet(pos_in, acc, dt: float): (float, float) = when isMainModule: let timeV = verlet(5.0, -10.0, 0.01) - echo "Time for Verlet integration is: ", timeV + echo "[#] Time for Verlet integration is:" + echo timeV let (timeSV, velSV) = stormerVerlet(5.0, -10.0, 0.01) - echo "Time for Stormer Verlet integration is: ", timeSV - echo "Velocity for Stormer Verlet integration is: ", velSV + echo "[#] Time for Stormer Verlet integration is:" + echo timeSV + echo "[#] Velocity for Stormer Verlet integration is:" + echo velSV let (timeVV, velVV) = velocityVerlet(5.0, -10.0, 0.01) - echo "Time for velocity Verlet integration is: ", timeVV - echo "Velocity for velocity Verlet integration is: ", velVV + echo "[#] Time for velocity Verlet integration is:" + echo timeVV + echo "[#] Velocity for velocity Verlet integration is:" + echo velVV diff --git a/contents/verlet_integration/code/python/verlet.py b/contents/verlet_integration/code/python/verlet.py index c921d1954..18dc627d3 100644 --- a/contents/verlet_integration/code/python/verlet.py +++ b/contents/verlet_integration/code/python/verlet.py @@ -35,21 +35,21 @@ def velocity_verlet(pos, acc, dt): def main(): time = verlet(5, -10, 0.01) - print("Verlet") - print("Time: {:.10f}".format(time)) - print() + print("[#] Time for Verlet integration is:") + print("{:.10f}".format(time)) time, vel = stormer_verlet(5, -10, 0.01) - print("Stormer-Verlet") - print("Time: {:.10f}".format(time)) - print("Velocity: {:.10f}".format(vel)) - print() + print("[#] Time for Stormer Verlet integration is:") + print("{:.10f}".format(time)) + print("[#] Velocity for Stormer Verlet integration is:") + print("{:.10f}".format(vel)) time, vel = velocity_verlet(5, -10, 0.01) - print("Velocity Verlet") - print("Time: {:.10f}".format(time)) - print("Velocity: {:.10f}".format(vel)) - print() + print("[#] Time for velocity Verlet integration is:") + print("{:.10f}".format(time)) + print("[#] Velocity for velocity Verlet integration is:") + print("{:.10f}".format(vel)) + if __name__ == '__main__': main() diff --git a/contents/verlet_integration/code/ruby/verlet.rb b/contents/verlet_integration/code/ruby/verlet.rb index 10246c6b7..4a6c38a48 100644 --- a/contents/verlet_integration/code/ruby/verlet.rb +++ b/contents/verlet_integration/code/ruby/verlet.rb @@ -27,7 +27,7 @@ def stormer_verlet(pos, acc, dt) vel += acc*dt end - return time + return time, vel end @@ -41,10 +41,21 @@ def velocity_verlet(pos, acc, dt) vel += acc*dt end - return time + return time, vel end +puts "[#] Time for Verlet integration is:" p verlet(5.0, -10, 0.01) -p stormer_verlet(5.0, -10, 0.01) -p velocity_verlet(5.0, -10, 0.01) + +time, vel = stormer_verlet(5.0, -10, 0.01) +puts "[#] Time for Stormer Verlet integration is:" +p time +puts "[#] Velocity for Stormer Verlet integration is:" +p vel + +time, vel = velocity_verlet(5.0, -10, 0.01) +puts "[#] Time for velocity Verlet integration is:" +p time +puts "[#] Velocity for velocity Verlet integration is:" +p vel diff --git a/contents/verlet_integration/code/rust/verlet.rs b/contents/verlet_integration/code/rust/verlet.rs index 490994da2..f765da864 100644 --- a/contents/verlet_integration/code/rust/verlet.rs +++ b/contents/verlet_integration/code/rust/verlet.rs @@ -49,13 +49,16 @@ fn main() { let (time_sv, vel_sv) = stormer_verlet(5.0, -10.0, 0.01); let (time_vv, vel_vv) = velocity_verlet(5.0, -10.0, 0.01); - println!("Time for original Verlet integration: {}", time_v); - println!( - "Time and velocity for Stormer Verlet integration: {}, {}", - time_sv, vel_sv - ); - println!( - "Time and velocity for velocity Verlet integration: {}, {}", - time_vv, vel_vv - ); + println!("[#] Time for Verlet integration is:"); + println!("{}", time_v); + + println!("[#] Time for Stormer Verlet integration is:"); + println!("{}", time_sv); + println!("[#] Velocity for Stormer Verlet integration is:"); + println!("{}", vel_sv); + + println!("[#] Time for velocity Verlet integration is:"); + println!("{}", time_vv); + println!("[#] Velocity for velocity Verlet integration is:"); + println!("{}", vel_vv); } diff --git a/contents/verlet_integration/code/swift/verlet.swift b/contents/verlet_integration/code/swift/verlet.swift index a241660a9..7991a0082 100644 --- a/contents/verlet_integration/code/swift/verlet.swift +++ b/contents/verlet_integration/code/swift/verlet.swift @@ -50,15 +50,20 @@ func velocityVerlet(pos: Double, acc: Double, dt: Double) -> (time: Double, vel: func main() { let verletTime = verlet(pos: 5.0, acc: -10.0, dt: 0.01) - print("Time for Verlet integration is: \(verletTime)") + print("[#] Time for Verlet integration is:") + print("\(verletTime)") let stormer = stormerVerlet(pos: 5.0, acc: -10.0, dt: 0.01); - print("Time for Stormer Verlet integration is: \(stormer.time)") - print("Velocity for Stormer Verlet integration is: \(stormer.vel)") + print("[#] Time for Stormer Verlet integration is:") + print("\(stormer.time)") + print("[#] Velocity for Stormer Verlet integration is:") + print("\(stormer.vel)") let velVerlet = velocityVerlet(pos: 5.0, acc: -10, dt: 0.01) - print("Time for velocity Verlet integration is: \(velVerlet.time)") - print("Velocity for velocity Verlet integration is: \(velVerlet.vel)") + print("[#] Time for velocity Verlet integration is:") + print("\(velVerlet.time)") + print("[#] Velocity for velocity Verlet integration is:") + print("\(velVerlet.vel)") } main()