Skip to content

Update C++ implementations for Verlet algorithms #299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 14, 2018

Conversation

berquist
Copy link
Member

Done according to #257.

@june128 june128 added the Implementation Edit This provides an edit to an algorithm implementation. (Code and maybe md files are edited.) label Jul 24, 2018
}

int main() {
double time, vel;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just print straight to console.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes the printing lines less cluttered, and it needs to be done anyway for the commands that return pairs, because I don't want to write operator<< for the pair type.

Copy link
Contributor

@mika314 mika314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM

#include <iostream>
#include <utility>

typedef std::pair<double, double> vpair;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using vpair = std::pair<double, double>;

is preferable way.

T.43: Prefer using over typedef for defining

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would prefer a simple struct with descriptive fields over the pair anyway. std::pair is alright for utility types but I dislike it for types that should have some semantic meaning.

}

int main() {
double time, vel;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use std::tie

@@ -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<double, double> stormer_verlet(double pos, double acc, double dt) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use vpair

}

double velocity_verlet(double pos, double acc, double dt) {
std::pair<double, double> velocity_verlet(double pos, double acc, double dt) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use vpair

<< time << std::endl;

timestep timestep_sv = stormer_verlet(5.0, -10, 0.01);
std::cout << "Time for Stormer Verlet integration is: " \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a backslash?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to use an explicit line continuation indicator even when not required.

@Gathros Gathros merged commit 87cabb1 into algorithm-archivists:master Sep 14, 2018
@berquist berquist deleted the verlet-cpp branch September 15, 2018 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation Edit This provides an edit to an algorithm implementation. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants