diff --git a/work/week8/particles/starting_point/csv_reader.cc b/work/week8/particles/starting_point/csv_reader.cc index e3c1f0a..1d270c0 100644 --- a/work/week8/particles/starting_point/csv_reader.cc +++ b/work/week8/particles/starting_point/csv_reader.cc @@ -1,47 +1,47 @@ #include "csv_reader.hh" #include "particles_factory_interface.hh" #include "planet.hh" #include #include /* -------------------------------------------------------------------------- */ CsvReader::CsvReader(const std::string& filename) : filename(filename) {} /* -------------------------------------------------------------------------- */ void CsvReader::read(System& system) { this->compute(system); } /* -------------------------------------------------------------------------- */ void CsvReader::compute(System& system) { std::ifstream is(filename.c_str()); std::string line; // Check if file is already open if (is.is_open() == false) { std::cerr << "Error: file not opened" << filename << std::endl; throw; } - + while (is.good()) { //Gets line from CSV file getline(is, line); //Copy line in sstr std::stringstream sstr(line); //Real line should be corresponding to: x y z v vx vy vz fx fy fz mass radius name // Check line structure before copying if (line.size() == 0) continue; // Creare Particle class and dynamic cast to Planet // std::unique_ptr p; // not to use, because the object will be destroyed by System // p.reset(new Planet()); std::shared_ptr p; p.reset(new Planet()); // Initialize Planet p->initself(sstr); // Add particle to the system system.addParticle(p); } // Close file is.close(); } /* -------------------------------------------------------------------------- */ diff --git a/work/week8/particles/starting_point/csv_writer.cc b/work/week8/particles/starting_point/csv_writer.cc index fdcbd1b..fd80080 100644 --- a/work/week8/particles/starting_point/csv_writer.cc +++ b/work/week8/particles/starting_point/csv_writer.cc @@ -1,20 +1,22 @@ #include "csv_writer.hh" #include #include /* -------------------------------------------------------------------------- */ CsvWriter::CsvWriter(const std::string& filename) : filename(filename) {} /* -------------------------------------------------------------------------- */ void CsvWriter::write(System& system) { this->compute(system); } /* -------------------------------------------------------------------------- */ void CsvWriter::compute(System& system) { - // std::ofstream os(this->filename); - // UInt nParticles = system.getListSize(); - // for (UInt i=0; ifilename); + UInt nParticles = system.getNbParticles(); + // std::cout << nParticles << std::endl; + for (UInt i=0; i #include #include /* -------------------------------------------------------------------------- */ int main(int argc, char** argv) { if (argc != 6) { std::cout << "Usage: " << argv[0] << " nsteps dump_freq input.csv particle_type timestep" << std::endl; std::cout << "\tparticle type can be: planet, ping_pong" << std::endl; std::exit(EXIT_FAILURE); } + // Parse arguments into stringstream + std::stringstream sstr; + for (int i = 1; i < argc; ++i) { + // std::cout << argv[i] << std::endl; + sstr << argv[i] << " "; + } + + // Set number of steps + Real nsteps; + sstr >> nsteps; + // Set dumper frequency + int freq; + sstr >> freq; + // Set filename + std::string filename; + sstr >> filename; + std::cout << filename << std::endl; + // Set particle's type + std::string particle_type; + sstr >> particle_type; + // Set time step + Real timestep; + sstr >> timestep; + + // Init system obj + System system; + // Init reader obj + CsvReader reader(filename); + // Read from file + reader.read(system); + + // Init writer obj + std::string saveFile = "saveFile.txt"; + CsvWriter writer(saveFile); + writer.write(system); + return EXIT_SUCCESS; } diff --git a/work/week8/particles/starting_point/particle.cc b/work/week8/particles/starting_point/particle.cc index 6483eda..455a614 100644 --- a/work/week8/particles/starting_point/particle.cc +++ b/work/week8/particles/starting_point/particle.cc @@ -1,13 +1,17 @@ #include "particle.hh" void Particle::printself(std::ostream& stream) const { + stream << position << " "; + stream << velocity << " "; + stream << force << " "; + stream << mass << " "; } /* -------------------------------------------------------------------------- */ void Particle::initself(std::istream& sstr) { sstr >> position; sstr >> velocity; sstr >> force; sstr >> mass; } diff --git a/work/week8/particles/starting_point/ping_pong_ball.cc b/work/week8/particles/starting_point/ping_pong_ball.cc index dde68da..2e44408 100644 --- a/work/week8/particles/starting_point/ping_pong_ball.cc +++ b/work/week8/particles/starting_point/ping_pong_ball.cc @@ -1,12 +1,14 @@ #include "ping_pong_ball.hh" /* -------------------------------------------------------------------------- */ void PingPongBall::printself(std::ostream& stream) const { + Particle::printself(stream); + stream << " " << radius; } /* -------------------------------------------------------------------------- */ void PingPongBall::initself(std::istream& sstr) { Particle::initself(sstr); sstr >> radius; } diff --git a/work/week8/particles/starting_point/planet.cc b/work/week8/particles/starting_point/planet.cc index 1429278..c120959 100644 --- a/work/week8/particles/starting_point/planet.cc +++ b/work/week8/particles/starting_point/planet.cc @@ -1,11 +1,13 @@ #include "planet.hh" void Planet::initself(std::istream &stream) { Particle::initself(stream); stream >> name; } /* -------------------------------------------------------------------------- */ void Planet::printself(std::ostream &stream) const { + Particle::printself(stream); + stream << name << " "; } diff --git a/work/week8/particles/starting_point/vector.cc b/work/week8/particles/starting_point/vector.cc index dcc176b..443c0a7 100644 --- a/work/week8/particles/starting_point/vector.cc +++ b/work/week8/particles/starting_point/vector.cc @@ -1,74 +1,75 @@ #include "vector.hh" Real& Vector::operator[](UInt i) { } const Real& Vector::operator[](UInt i) const { } Real Vector::squaredNorm() const { } /* -------------------------------------------------------------------------- */ Vector& Vector::operator+=(const Vector& vec) { return *this; } Vector& Vector::operator-=(const Vector& vec) { return *this; } Vector& Vector::operator*=(Real val) { return *this; } Vector& Vector::operator/=(Real val) { return *this; } /* -------------------------------------------------------------------------- */ Vector& Vector::operator=(const Vector& vec) { return *this; } Vector& Vector::operator=(Real val) { return *this; } /* -------------------------------------------------------------------------- */ Vector operator+(const Vector& a, const Vector& b) { } Vector operator-(const Vector& a, const Vector& b) { } Vector operator*(const Vector& a, Real val) { } Vector operator*(Real val, const Vector& a) { return a * val; } Vector operator/(const Vector& a, Real val) { } /* -------------------------------------------------------------------------- */ /// standard output stream operator std::ostream& operator<<(std::ostream& stream, const Vector& _this) { for (UInt i = 0; i < _this.dim-1; ++i) { stream << _this.values[i] << " "; } + stream << _this.values[_this.dim-1]; return stream; } /* -------------------------------------------------------------------------- */ /// standard input stream operator std::istream& operator>>(std::istream& stream, Vector& _this) { - for (UInt i = 0; i < _this.dim-1; ++i) { + for (UInt i = 0; i < _this.dim; ++i) { stream >> _this.values[i]; } return stream; }