Unverified Commit 86ad1ab7 authored by Guillaume Anciaux's avatar Guillaume Anciaux
Browse files

adding the solution of week8

parent fe7ebe92
#include "system.hh"
Particle& System::getParticle(UInt i) {
return *list_particles[i];
}
/* -------------------------------------------------------------------------- */
void System::addParticle(const std::shared_ptr<Particle>& new_particle) {
list_particles.push_back(new_particle);
}
/* -------------------------------------------------------------------------- */
UInt System::getNbParticles() {
return list_particles.size();
}
#ifndef __SYSTEM__HH__
#define __SYSTEM__HH__
/* -------------------------------------------------------------------------- */
#include "my_types.hh"
#include "particle.hh"
#include <memory>
#include <vector>
/* -------------------------------------------------------------------------- */
//! Container for particles
class System {
/*
* No need for constructor/destructor with std::unique_ptr
*/
// List of particle pointers
using ParticleList = std::vector<std::shared_ptr<Particle>>;
// Methods
public:
//! Remove particle from vector
void removeParticle(UInt particle);
//! Get particle for specific id
Particle& getParticle(UInt i);
//! Add a particle to the system
void addParticle(const std::shared_ptr<Particle>& new_particle);
//! Get number of particles
UInt getNbParticles();
//! Iterator class to erase the unique pointer on Particle
struct iterator : ParticleList::iterator {
iterator(const ParticleList::iterator& it) : ParticleList::iterator(it) {}
//! Access the underlying particle
Particle& operator*() {
return *ParticleList::iterator::operator*();
}
};
// Iterators
public:
auto begin() { return iterator(list_particles.begin()); }
auto end() { return iterator(list_particles.end()); }
private:
ParticleList list_particles;
};
/* -------------------------------------------------------------------------- */
#endif //__SYSTEM__HH__
#include "system_evolution.hh"
#include "csv_writer.hh"
/* -------------------------------------------------------------------------- */
#include <iomanip>
#include <sstream>
/* -------------------------------------------------------------------------- */
SystemEvolution::SystemEvolution(std::unique_ptr<System> system)
: system(std::move(system)) {}
/* -------------------------------------------------------------------------- */
void SystemEvolution::evolve() {
for (UInt i = 0; i < nsteps; ++i) {
for (auto & compute : computes)
compute->compute(*system);
if (i % freq == 0) {
std::stringstream sstr;
sstr << "dumps/step-" << std::setfill('0') << std::setw(5) << i << ".csv";
CsvWriter dumper(sstr.str());
dumper.write(*system);
}
}
}
/* -------------------------------------------------------------------------- */
void SystemEvolution::addCompute(const std::shared_ptr<Compute>& compute) {
computes.push_back(compute);
}
/* -------------------------------------------------------------------------- */
void SystemEvolution::setNSteps(UInt nsteps) { this->nsteps = nsteps; }
/* -------------------------------------------------------------------------- */
void SystemEvolution::setDumpFreq(UInt freq) { this->freq = freq; }
/* -------------------------------------------------------------------------- */
System& SystemEvolution::getSystem() { return *system; }
/* -------------------------------------------------------------------------- */
#ifndef __SYSTEM_EVOLUTION__HH__
#define __SYSTEM_EVOLUTION__HH__
/* -------------------------------------------------------------------------- */
#include "compute.hh"
#include "system.hh"
/* -------------------------------------------------------------------------- */
//! Manager for system evolution
class SystemEvolution {
/* ------------------------------------------------------------------------ */
/* Constructors/Destructors */
/* ------------------------------------------------------------------------ */
public:
//! Construct using existing system (takes ownership)
SystemEvolution(std::unique_ptr<System> system);
/* ------------------------------------------------------------------------ */
/* Methods */
/* ------------------------------------------------------------------------ */
public:
//! Evolve all time steps
void evolve();
//! Add compute to list of computes
void addCompute(const std::shared_ptr<Compute>& compute);
//! Get the system object
System& getSystem();
void setNSteps(UInt nsteps);
void setDumpFreq(UInt freq);
/* ------------------------------------------------------------------------ */
/* Members */
/* ------------------------------------------------------------------------ */
protected:
std::vector<std::shared_ptr<Compute>> computes;
std::unique_ptr<System> system;
UInt nsteps, freq;
};
/* -------------------------------------------------------------------------- */
#endif //__SYSTEM_EVOLUTION__HH__
#include "vector.hh"
Real& Vector::operator[](UInt i) {
return values[i];
}
const Real& Vector::operator[](UInt i) const {
return values[i];
}
Real Vector::squaredNorm() const {
Real res = 0;
for (auto& val : values) {
res += val * val;
}
return res;
}
/* -------------------------------------------------------------------------- */
Vector& Vector::operator+=(const Vector& vec) {
for (UInt i = 0; i < dim; ++i)
values[i] += vec[i];
return *this;
}
Vector& Vector::operator-=(const Vector& vec) {
for (UInt i = 0; i < dim; ++i)
values[i] -= vec[i];
return *this;
}
Vector& Vector::operator*=(Real val) {
for (auto& v : values)
v *= val;
return *this;
}
Vector& Vector::operator/=(Real val) {
for (auto& v : values)
v /= val;
return *this;
}
/* -------------------------------------------------------------------------- */
Vector& Vector::operator=(const Vector& vec) {
std::copy(vec.values.begin(), vec.values.end(), values.begin());
return *this;
}
Vector& Vector::operator=(Real val) {
std::fill(values.begin(), values.end(), val);
return *this;
}
/* -------------------------------------------------------------------------- */
Vector operator+(const Vector& a, const Vector& b) {
Vector res(a);
res += b;
return res;
}
Vector operator-(const Vector& a, const Vector& b) {
Vector res(a);
res -= b;
return res;
}
Vector operator*(const Vector& a, Real val) {
Vector res(a);
res *= val;
return res;
}
Vector operator*(Real val, const Vector& a) {
return a * val;
}
Vector operator/(const Vector& a, Real val) {
Vector res(a);
res /= val;
return res;
}
/* -------------------------------------------------------------------------- */
/// standard output stream operator
std::ostream& operator<<(std::ostream& stream, const Vector& _this) {
for (auto& v : _this.values)
stream << v << " ";
return stream;
}
/* -------------------------------------------------------------------------- */
/// standard input stream operator
std::istream& operator>>(std::istream& stream, Vector& _this) {
for (auto& v : _this.values)
stream >> v;
return stream;
}
#ifndef __VECTOR__HH__
#define __VECTOR__HH__
/* -------------------------------------------------------------------------- */
#include "my_types.hh"
/* -------------------------------------------------------------------------- */
#include <array>
/**
* @brief 3D Vector class
*
* Note that no constructor is needed for this class
*/
class Vector {
public:
//! Dimension of vector
static const UInt dim = 3;
// Methods
public:
//! access given component
Real& operator[](UInt i);
//! access given component (const)
const Real& operator[](UInt i) const;
//! square of euclidian norm
Real squaredNorm() const;
// Operators that make sense for vectors
Vector& operator+=(const Vector& vec);
Vector& operator-=(const Vector& vec);
Vector& operator*=(Real val);
Vector& operator/=(Real val);
//! Copy operator
Vector& operator=(const Vector& vec);
//! Assign a value
Vector& operator=(Real val);
public:
//! Output to stream
friend std::ostream& operator<<(std::ostream& stream, const Vector& _this);
//! Initialize from stream
friend std::istream& operator>>(std::istream& stream, Vector& _this);
private:
//! Vector values (initilized to zero by default)
std::array<Real, dim> values = {0};
};
/* -------------------------------------------------------------------------- */
/* Separate function definitions */
/* -------------------------------------------------------------------------- */
/// standard output stream operator
std::ostream& operator<<(std::ostream& stream, const Vector& _this);
/// standard input stream operator
std::istream& operator>>(std::istream& stream, Vector& _this);
// We define the operators +, -, *, / with vectors and scalars
Vector operator+(const Vector & a, const Vector& b);
Vector operator-(const Vector & a, const Vector& b);
// Symmetry of multiplication
Vector operator*(const Vector & a, Real val);
Vector operator*(Real val, const Vector & a);
// For convenience
Vector operator/(const Vector & a, Real val);
#endif //__VECTOR__HH__
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment