From 10ebadfe7a1500b79b9f320dec9f4b00bd769e73 Mon Sep 17 00:00:00 2001 From: Eric Berquist Date: Tue, 24 Jul 2018 14:57:00 -0400 Subject: [PATCH 1/3] Adjust C++ code for Verlet according to #257 --- .../verlet_integration/code/c++/verlet.cpp | 56 ++++++++++++++----- .../verlet_integration/verlet_integration.md | 6 +- 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/contents/verlet_integration/code/c++/verlet.cpp b/contents/verlet_integration/code/c++/verlet.cpp index 9451c7336..6b3736eda 100644 --- a/contents/verlet_integration/code/c++/verlet.cpp +++ b/contents/verlet_integration/code/c++/verlet.cpp @@ -1,6 +1,9 @@ +#include #include +#include + +typedef std::pair vpair; -// Simple function for velocity-verlet double verlet(double pos, double acc, double dt) { double prev_pos = pos; @@ -16,8 +19,7 @@ double verlet(double pos, double acc, double dt) { return time; } -// Simple function for stormer-verlet -double stormer_verlet(double pos, double acc, double dt) { +std::pair stormer_verlet(double pos, double acc, double dt) { double prev_pos = pos; double time = 0; @@ -28,14 +30,15 @@ double stormer_verlet(double pos, double acc, double dt) { prev_pos = pos; pos = next_pos; - // The acceleration is constant, so the velocity is straightforward + // The acceleration is constant, so the velocity is + // straightforward vel += acc * dt; } - return time; + return std::make_pair(time, vel); } -double velocity_verlet(double pos, double acc, double dt) { +std::pair velocity_verlet(double pos, double acc, double dt) { double time = 0; double vel = 0; @@ -45,18 +48,41 @@ double velocity_verlet(double pos, double acc, double dt) { vel += acc * dt; } - return time; + return std::make_pair(time, vel); } int main() { + double time, vel; + vpair time_vel_pair; + + std::cout << std::fixed << std::setprecision(8); + + // Note that depending on the simulation, you might want to have the + // Verlet loop outside. + + // For example, if your acceleration chages as a function of time, + // you might need to also change the acceleration to be read into + // each of these functions. + + time = verlet(5.0, -10, 0.01); + std::cout << "Time for Verlet integration is: " \ + << time << std::endl; + + time_vel_pair = stormer_verlet(5.0, -10, 0.01); + time = time_vel_pair.first; + vel = time_vel_pair.second; + std::cout << "Time for Stormer Verlet integration is: " \ + << time << std::endl; + std::cout << "Velocity for Stormer Verlet integration is: " \ + << vel << std::endl; - // Note that depending on the simulation, you might want to have the verlet - // loop outside. + time_vel_pair = velocity_verlet(5.0, -10, 0.01); + time = time_vel_pair.first; + vel = time_vel_pair.second; + std::cout << "Time for velocity Verlet integration is: " \ + << time << std::endl; + std::cout << "Velocity for velocity Verlet integration is: " \ + << vel << std::endl; - // For example, if your acceleration chages as a function of time, you might - // need to also change the acceleration to be read into each of these - // functions - std::cout << verlet(5.0, -10, 0.01) << std::endl; - std::cout << stormer_verlet(5.0, -10, 0.01) << std::endl; - std::cout << velocity_verlet(5.0, -10, 0.01) << std::endl; + return 0; } diff --git a/contents/verlet_integration/verlet_integration.md b/contents/verlet_integration/verlet_integration.md index e1b28c7c9..1aa9db945 100644 --- a/contents/verlet_integration/verlet_integration.md +++ b/contents/verlet_integration/verlet_integration.md @@ -33,7 +33,7 @@ Here is what it looks like in code: {% sample lang="jl" %} [import:1-13, lang:"julia"](code/julia/verlet.jl) {% sample lang="cpp" %} -[import:4-17, lang:"c_cpp"](code/c++/verlet.cpp) +[import:7-20, lang:"c_cpp"](code/c++/verlet.cpp) {% sample lang="c" %} [import:3-14, lang:"c_cpp"](code/c/verlet.c) {% sample lang="java" %} @@ -79,7 +79,7 @@ Here's what it looks like in code: {% sample lang="jl" %} [import:15-31, lang:"julia"](code/julia/verlet.jl) {% sample lang="cpp" %} -[import:20-36, lang:"c_cpp"](code/c++/verlet.cpp) +[import:22-39, lang:"c_cpp"](code/c++/verlet.cpp) {% sample lang="c" %} [import:16-31, lang:"c_cpp"](code/c/verlet.c) {% sample lang="java" %} @@ -136,7 +136,7 @@ Here is the velocity Verlet method in code: {% sample lang="jl" %} [import:33-45, lang:"julia"](code/julia/verlet.jl) {% sample lang="cpp" %} -[import:38-49, lang:"c_cpp"](code/c++/verlet.cpp) +[import:41-52, lang:"c_cpp"](code/c++/verlet.cpp) {% sample lang="c" %} [import:33-43, lang:"c_cpp"](code/c/verlet.c) {% sample lang="java" %} From fc192e297c686e39ee13a3e586aecaabbd85fe4f Mon Sep 17 00:00:00 2001 From: Eric Berquist Date: Sun, 12 Aug 2018 13:49:10 -0400 Subject: [PATCH 2/3] use tie and swap typedef for using --- contents/verlet_integration/code/c++/verlet.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/contents/verlet_integration/code/c++/verlet.cpp b/contents/verlet_integration/code/c++/verlet.cpp index 6b3736eda..128a4a928 100644 --- a/contents/verlet_integration/code/c++/verlet.cpp +++ b/contents/verlet_integration/code/c++/verlet.cpp @@ -2,7 +2,7 @@ #include #include -typedef std::pair vpair; +using vpair = std::pair; double verlet(double pos, double acc, double dt) { @@ -19,7 +19,7 @@ double verlet(double pos, double acc, double dt) { return time; } -std::pair stormer_verlet(double pos, double acc, double dt) { +vpair stormer_verlet(double pos, double acc, double dt) { double prev_pos = pos; double time = 0; @@ -38,7 +38,7 @@ std::pair stormer_verlet(double pos, double acc, double dt) { return std::make_pair(time, vel); } -std::pair velocity_verlet(double pos, double acc, double dt) { +vpair velocity_verlet(double pos, double acc, double dt) { double time = 0; double vel = 0; @@ -53,7 +53,6 @@ std::pair velocity_verlet(double pos, double acc, double dt) { int main() { double time, vel; - vpair time_vel_pair; std::cout << std::fixed << std::setprecision(8); @@ -68,21 +67,18 @@ int main() { std::cout << "Time for Verlet integration is: " \ << time << std::endl; - time_vel_pair = stormer_verlet(5.0, -10, 0.01); - time = time_vel_pair.first; - vel = time_vel_pair.second; + std::tie(time, vel) = stormer_verlet(5.0, -10, 0.01); std::cout << "Time for Stormer Verlet integration is: " \ << time << std::endl; std::cout << "Velocity for Stormer Verlet integration is: " \ << vel << std::endl; - time_vel_pair = velocity_verlet(5.0, -10, 0.01); - time = time_vel_pair.first; - vel = time_vel_pair.second; + std::tie(time, vel) = velocity_verlet(5.0, -10, 0.01); std::cout << "Time for velocity Verlet integration is: " \ << time << std::endl; std::cout << "Velocity for velocity Verlet integration is: " \ << vel << std::endl; return 0; + } From 35365e8615d18b1c4363205ccfd6faadf73cc0ad Mon Sep 17 00:00:00 2001 From: Eric Berquist Date: Sun, 12 Aug 2018 13:57:25 -0400 Subject: [PATCH 3/3] keep results in a struct rather than a pair --- .../verlet_integration/code/c++/verlet.cpp | 30 +++++++++---------- .../verlet_integration/verlet_integration.md | 6 ++-- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/contents/verlet_integration/code/c++/verlet.cpp b/contents/verlet_integration/code/c++/verlet.cpp index 128a4a928..473f92ee9 100644 --- a/contents/verlet_integration/code/c++/verlet.cpp +++ b/contents/verlet_integration/code/c++/verlet.cpp @@ -1,8 +1,10 @@ #include #include -#include -using vpair = std::pair; +struct timestep { + double time; + double vel; +}; double verlet(double pos, double acc, double dt) { @@ -19,7 +21,7 @@ double verlet(double pos, double acc, double dt) { return time; } -vpair stormer_verlet(double pos, double acc, double dt) { +timestep stormer_verlet(double pos, double acc, double dt) { double prev_pos = pos; double time = 0; @@ -35,10 +37,10 @@ vpair stormer_verlet(double pos, double acc, double dt) { vel += acc * dt; } - return std::make_pair(time, vel); + return timestep { time, vel }; } -vpair velocity_verlet(double pos, double acc, double dt) { +timestep velocity_verlet(double pos, double acc, double dt) { double time = 0; double vel = 0; @@ -48,12 +50,10 @@ vpair velocity_verlet(double pos, double acc, double dt) { vel += acc * dt; } - return std::make_pair(time, vel); + return timestep { time, vel }; } int main() { - double time, vel; - std::cout << std::fixed << std::setprecision(8); // Note that depending on the simulation, you might want to have the @@ -63,21 +63,21 @@ int main() { // you might need to also change the acceleration to be read into // each of these functions. - time = verlet(5.0, -10, 0.01); + double time = verlet(5.0, -10, 0.01); std::cout << "Time for Verlet integration is: " \ << time << std::endl; - std::tie(time, vel) = stormer_verlet(5.0, -10, 0.01); + timestep timestep_sv = stormer_verlet(5.0, -10, 0.01); std::cout << "Time for Stormer Verlet integration is: " \ - << time << std::endl; + << timestep_sv.time << std::endl; std::cout << "Velocity for Stormer Verlet integration is: " \ - << vel << std::endl; + << timestep_sv.vel << std::endl; - std::tie(time, vel) = velocity_verlet(5.0, -10, 0.01); + timestep timestep_vv = velocity_verlet(5.0, -10, 0.01); std::cout << "Time for velocity Verlet integration is: " \ - << time << std::endl; + << timestep_vv.time << std::endl; std::cout << "Velocity for velocity Verlet integration is: " \ - << vel << std::endl; + << timestep_vv.vel << std::endl; return 0; diff --git a/contents/verlet_integration/verlet_integration.md b/contents/verlet_integration/verlet_integration.md index 1aa9db945..21ae932e5 100644 --- a/contents/verlet_integration/verlet_integration.md +++ b/contents/verlet_integration/verlet_integration.md @@ -33,7 +33,7 @@ Here is what it looks like in code: {% sample lang="jl" %} [import:1-13, lang:"julia"](code/julia/verlet.jl) {% sample lang="cpp" %} -[import:7-20, lang:"c_cpp"](code/c++/verlet.cpp) +[import:9-22, lang:"c_cpp"](code/c++/verlet.cpp) {% sample lang="c" %} [import:3-14, lang:"c_cpp"](code/c/verlet.c) {% sample lang="java" %} @@ -79,7 +79,7 @@ Here's what it looks like in code: {% sample lang="jl" %} [import:15-31, lang:"julia"](code/julia/verlet.jl) {% sample lang="cpp" %} -[import:22-39, lang:"c_cpp"](code/c++/verlet.cpp) +[import:24-41, lang:"c_cpp"](code/c++/verlet.cpp) {% sample lang="c" %} [import:16-31, lang:"c_cpp"](code/c/verlet.c) {% sample lang="java" %} @@ -136,7 +136,7 @@ Here is the velocity Verlet method in code: {% sample lang="jl" %} [import:33-45, lang:"julia"](code/julia/verlet.jl) {% sample lang="cpp" %} -[import:41-52, lang:"c_cpp"](code/c++/verlet.cpp) +[import:43-54, lang:"c_cpp"](code/c++/verlet.cpp) {% sample lang="c" %} [import:33-43, lang:"c_cpp"](code/c/verlet.c) {% sample lang="java" %}