Insights Blog
-- Browse All Articles --
Physics Articles
Physics Tutorials
Physics Guides
Physics FAQ
Math Articles
Math Tutorials
Math Guides
Math FAQ
Education Articles
Education Guides
Bio/Chem Articles
Technology Guides
Computer Science Tutorials
Forums
Chemistry
Biology and Medical
Earth Sciences
Computer Science
Computing and Technology
DIY Projects
Trending
Featured Threads
Log in
Register
What's new
Search
Search
Search titles only
By:
Chemistry
Biology and Medical
Earth Sciences
Computer Science
Computing and Technology
DIY Projects
Menu
Log in
Register
Navigation
More options
Contact us
Close Menu
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Forums
Other Sciences
Programming and Computer Science
Velocity Verlet C++ implementation
Reply to thread
Message
[QUOTE="madsmh, post: 5811971, member: 276462"] I have been working on implementing a solar system system simulator in C++ - but am getting incorrect results on the order of 10^10 km, and it seems that the planets are moving directly away from the sun. I suspect that there is a mistake in the integrator (Velocity Verlet) that I have posted below. It would be appreciated if I could get someone to check that, assuming that the rest of the program is correct, the integrator is working. The code should be self-explanatory. Thanks! [code=C++]void verlet(System& system, Trajectory& trajectory, double delta){ long n = trajectory.get_number_of_rows(); long n_bodies = system.get_number_of_bodies(); double delta2 = pow(delta, 2); std::cout << "Starting integrator." << std::endl; for (int i = 0; i < n; ++i) { if(i == 0){ std::vector<Vector3 > x0 = system.get_positions(); std::vector<Vector3> v0 = system.get_velocities(); trajectory.set_position(x0, v0); system.set_positions(x0); system.set_velocities(v0); } else { std::vector<Vector3> x0 = trajectory.get_positions_at_index(i-1); std::vector<Vector3> v0 = trajectory.get_velocities_at_index(i-1); std::vector<Vector3> a0 = system.get_accelerations(); std::vector<Vector3> x1 {}; for (long j = 0; j < n_bodies; ++j) { x1.emplace_back(x0[j] + v0[j] * delta + delta2 * 0.5 * a0[j]); } system.set_positions(x1); std::vector<Vector3 > a1 = system.get_accelerations(); std::vector<Vector3 > v1; for (long k = 0; k < n_bodies; ++k) { v1.emplace_back(v0[k] + 0.5 *( a0[k] + a1[k]) * delta ); } system.set_velocities(v1); trajectory.set_position(x1, v1); } } std::cout << "Integration finished." << std::endl; } [/code] [/QUOTE]
Insert quotes…
Post reply
Forums
Other Sciences
Programming and Computer Science
Velocity Verlet C++ implementation
Back
Top