seems the compiler newsgroups are only posted to every couple of days
or so, so I may as well post the code here, been through it a few times
myself and have had no luck in spotting anything untoward:
Hi Cleverbum,
BTW I think you need to pass the arguments by refernece in these
functions:
class particle {...
friend void calculate_forces(particle&, particle&);
friend void single_particle_forces(particle&);
};
If you dont you will get a copy of particles passed into the functions
and your original particles will remain unchanged.
I have been implementing a library dealing with physical quantities and
your code is just the type of example I am looking for to test the code
and improve it.
http://tinyurl.com/7m5l8
Anyway FWIW Below ---^--> is what the code would look like in my pqs
library.
(Please note that I have had to modify the library code from the
version of pqs above, by adding an array operator to the three_d vect
class and also removing the constraint on multiplication/divison of
vectors by scalars to arithmetic types only and added assignment
operators (so it wont compile with the latest released version
pqs_3_1_0) but this is just the type of code that can help me find out
what is missing.
I am wondering if it would be OK to use this as an example in a future
version of the pqs library, with due attribution (to Cleverbum?) of
course? ( Note also the license, which is the only one acceptable to
boost FWIW)
regards
Andy Little
-------------------------------------------
// Copyright Cleverbum 2005
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
//
http://www.boost.org/LICENSE_1_0.txt)
/*
models particle(s) settling under gravity in three dimensions.
also includes inter-particle gravity
*/
#include <ctime>
#include <iostream>
#include <boost/shared_array.hpp>
#include <boost/pqs/t1_quantity/types/out/length.hpp>
#include <boost/pqs/t1_quantity/types/out/velocity.hpp>
#include <boost/pqs/t1_quantity/types/out/force.hpp>
#include <boost/pqs/t1_quantity/types/out/mass.hpp>
#include <boost/pqs/t1_quantity/types/out/time.hpp>
#include <boost/pqs/t1_quantity/types/out/acceleration.hpp>
#include <boost/pqs/t1_quantity/constants/gravitational_constant.hpp>
#include <boost/pqs/three_d/vect_out.hpp>
#include <boost/pqs/angle/out/angle.hpp>
namespace pqs = boost:
qs;
const int num_particles = 1; // number of particles
const pqs::time_<int>::s max_time(100);
const pqs::time_<int>::s timeStep(1);
class particle{
private:
pqs::three_d::vect<pqs::length::m> pos;
pqs::three_d::vect<pqs::velocity::m_div_s> vel;
pqs::three_d::vect<pqs::force::N> frc;
pqs::mass::kg mass;
bool hard;
// not referenced?
// pqs::angle::rad rad;
public:
particle()
// note frc and vel are default initialised to 0,0,0 in pqs
:mass(1.0)/*,rad(1.0)*/,hard(true)
{
srand(time(0));
pos.x = pqs::length::mm(rand()%1000);
pos.y = pqs::length::mm(rand()%1000);
pos.z = pqs::length::mm(rand()%1000);
}
void move(); // uses speeds saved within particle object
void accelerate(); // uses forces stored inside particle object
std::string toString(){
std:
stringstream os;
os << pos.x << '\t'
<< pos.y << '\t'
<< pos.z << '\t'
<< vel.x << '\t'
<< vel.y << '\t'
<< vel.z << '\t';
return os.str();
} ;
friend void calculate_forces(particle&, particle&);
friend void single_particle_forces(particle&);
};
int main(){
// See
http://www.boost.org/libs/smart_ptr/shared_array.htm
// for documentation.
boost::shared_array<
particle
> myParticles(new particle[num_particles]);
for( pqs::time_<int>::s simulation_time
= pqs::time_<int>::s(0);
simulation_time < max_time;
simulation_time += timeStep){
for(int i=0; i < num_particles; ++i){
if(num_particles > 1){
for(int j = i+1; j < num_particles; ++j){
calculate_forces(myParticles
, myParticles[j]);
}
}
single_particle_forces(myParticles);
myParticles.accelerate();
myParticles.move();
std::cout << i << myParticles.toString();
}
}
}
void particle::move(){
this->pos += this->vel * timeStep;
}
void particle::accelerate(){
this->vel += this->frc / this->mass * timeStep;
}
void calculate_forces(particle& x, particle& y){
for(int i = 0;i < 3;++i){
//Gravitational attraction between the bodies
//used to determine direction of force
int mySign =(y.pos > x.pos)? -1:1;
x.frc = mySign
* pqs:hysics::gravitational_constant::G
* x.mass * y.mass / pqs:ow<2>(y.pos - x.pos);
y.frc = -1 * mySign
* pqs:hysics::gravitational_constant::G
* x.mass * y.mass / pqs:ow<2>( y.pos - x.pos);
}
}
void single_particle_forces(particle& x){
//gravity acts negative on z axis
x.frc.y -= x.mass * pqs::acceleration::g;
}