Skip to main content

Timeline for Simple n-body class in C++

Current License: CC BY-SA 4.0

11 events
when toggle format what by license comment
Oct 25, 2019 at 20:38 comment added G. Sliepen You could use std::valarray<> as @Davislor suggested, but that lacks more interesting functions, like calculating the length of a vector. Math libraries will do that for you.
Oct 25, 2019 at 20:32 comment added Gilfoyle Can I also use std::array<double, DIM> instead of a vector math library as you suggested? Could I then also get rid of the for loop?
Oct 25, 2019 at 19:52 comment added G. Sliepen Yes, that's correct.
Oct 25, 2019 at 19:05 comment added Gilfoyle Am I correct in assuming that I then have to use void Particle::force(Particle& other) and inside force() I remove all i-> and replace j-> by other->?
Oct 25, 2019 at 18:28 comment added G. Sliepen Once you've written void force(Particle &other) as a member function of struct Particle, change force(&p[i], &p[j]) to p[i].force(p[j]) inside the nested loop. Here you should probably keep using int i and int j as iterator variables, as that's the easiest to ensure each pair is only visited once.
Oct 25, 2019 at 16:24 comment added Gilfoyle Can you please outline how you would compute the force with your approach? I have difficulties since I have in the case of the force computation a nested loop and I am not sure how to do that in this case.
Oct 25, 2019 at 16:14 comment added G. Sliepen I recommend you use auto whenever the type is already clear from the context, or when you don't care about the type. Here, it should be obvious that p is a Particle, because it's an element of particles. Using auto in these cases avoids repeating types unnecessarily, and potentially avoids mistakes.
Oct 25, 2019 at 15:36 comment added Gilfoyle Is it recommended to use for (auto &p: particles) or is for (Particle &p: particles) better? When should I not use auto?
Oct 25, 2019 at 7:51 comment added Gilfoyle I tried to incorporate first the second part of your answer but get the following errors: nbody.cpp: In member function ‘void Particle::update_position(double)’: nbody.cpp:47:33: error: ‘p’ was not declared in this scope const double a = dt * 0.5 / p->m; ^ nbody.cpp: In member function ‘void Nbody::comp_position()’: nbody.cpp:55:19: error: ‘particles’ was not declared in this scope for (auto &p: particles) {
Oct 24, 2019 at 19:42 history edited G. Sliepen CC BY-SA 4.0
added 872 characters in body
Oct 24, 2019 at 19:28 history answered G. Sliepen CC BY-SA 4.0