diff --git a/.DS_Store b/.DS_Store index d119b66..4eefc78 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/.DS_Store b/Homework3/.DS_Store similarity index 74% copy from .DS_Store copy to Homework3/.DS_Store index d119b66..0baad8e 100644 Binary files a/.DS_Store and b/Homework3/.DS_Store differ diff --git a/Homework3/CMakeLists.txt b/Homework3/CMakeLists.txt new file mode 100644 index 0000000..51bde9a --- /dev/null +++ b/Homework3/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 2.6) + +set(CMAKE_CXX_STANDARD 14) + +add_subdirectory(src/) \ No newline at end of file diff --git a/Homework3/README.md b/Homework3/README.md new file mode 100644 index 0000000..7b53d8d --- /dev/null +++ b/Homework3/README.md @@ -0,0 +1,34 @@ + +# SP4E - Homework 3 + + + +---- + +## General Info + + + +---- + +## Project Description + + + +---- + +## Executable File + + + +---- + +## Running + + + +---- + +## Concluding remarks + + diff --git a/Homework3/src/CMakeLists.txt b/Homework3/src/CMakeLists.txt new file mode 100644 index 0000000..de1903d --- /dev/null +++ b/Homework3/src/CMakeLists.txt @@ -0,0 +1,28 @@ + +cmake_minimum_required (VERSION 2.6) +project (Particles) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(particles + main.cc + vector.cc + compute_boundary.cc + compute_verlet_integration.cc + particle.cc + planet.cc + compute_gravity.cc + csv_reader.cc + particles_factory_interface.cc + planets_factory.cc + compute_contact.cc + compute_kinetic_energy.cc + csv_writer.cc + system.cc + compute_energy.cc + compute_potential_energy.cc + ping_pong_ball.cc + system_evolution.cc + ping_pong_balls_factory.cc + compute_interaction.cc) + diff --git a/Homework3/src/compute.hh b/Homework3/src/compute.hh new file mode 100644 index 0000000..7ab83db --- /dev/null +++ b/Homework3/src/compute.hh @@ -0,0 +1,23 @@ +#ifndef __COMPUTE__HH__ +#define __COMPUTE__HH__ + +/* -------------------------------------------------------------------------- */ +#include "system.hh" +/* -------------------------------------------------------------------------- */ + +//! Base class for all compute +class Compute { +public: + //! Virtual destructor needed because we have subclasses + virtual ~Compute() = default; + + /* ------------------------------------------------------------------------ */ + /* Methods */ + /* ------------------------------------------------------------------------ */ +public: + //! Compute is pure virtual + virtual void compute(System& system) = 0; +}; + +/* -------------------------------------------------------------------------- */ +#endif //__COMPUTE__HH__ diff --git a/Homework3/src/compute_boundary.cc b/Homework3/src/compute_boundary.cc new file mode 100644 index 0000000..1b23990 --- /dev/null +++ b/Homework3/src/compute_boundary.cc @@ -0,0 +1,17 @@ +#include "compute_boundary.hh" +/* -------------------------------------------------------------------------- */ +ComputeBoundary::ComputeBoundary(const Vector& xmin, const Vector& xmax) + : xmin(xmin), xmax(xmax) { + Vector d = xmax - xmin; + for (UInt i = 0; i < Vector::dim; ++i) + if (d[i] < 0) { + std::cout << "XMax and XMin do not form a domain range" << std::endl; + std::exit(1); + } +} +/* -------------------------------------------------------------------------- */ + +void ComputeBoundary::compute(System& system) { +} + +/* -------------------------------------------------------------------------- */ diff --git a/Homework3/src/compute_boundary.hh b/Homework3/src/compute_boundary.hh new file mode 100644 index 0000000..c262cbc --- /dev/null +++ b/Homework3/src/compute_boundary.hh @@ -0,0 +1,24 @@ +#ifndef __COMPUTE_BOUNDARY__HH__ +#define __COMPUTE_BOUNDARY__HH__ + +/* -------------------------------------------------------------------------- */ +#include "compute.hh" +/* -------------------------------------------------------------------------- */ + +//! Compute interaction with simulation box +class ComputeBoundary : public Compute { + // Constructors/Destructors +public: + ComputeBoundary(const Vector& xmin, const Vector& xmax); + + // Methods +public: + void compute(System& system) override; + + // Members +protected: + Vector xmin, xmax; +}; + +/* -------------------------------------------------------------------------- */ +#endif //__COMPUTE_BOUNDARY__HH__ diff --git a/Homework3/src/compute_contact.cc b/Homework3/src/compute_contact.cc new file mode 100644 index 0000000..2d5820f --- /dev/null +++ b/Homework3/src/compute_contact.cc @@ -0,0 +1,9 @@ +#include "compute_contact.hh" +#include "ping_pong_ball.hh" +#include +/* -------------------------------------------------------------------------- */ +void ComputeContact::setPenalty(Real penalty) { +} +/* -------------------------------------------------------------------------- */ +void ComputeContact::compute(System& system) { +} diff --git a/Homework3/src/compute_contact.hh b/Homework3/src/compute_contact.hh new file mode 100644 index 0000000..03ab721 --- /dev/null +++ b/Homework3/src/compute_contact.hh @@ -0,0 +1,26 @@ +#ifndef __COMPUTE_CONTACT__HH__ +#define __COMPUTE_CONTACT__HH__ + +/* -------------------------------------------------------------------------- */ +#include "compute_interaction.hh" + +//! Compute contact interaction between ping-pong balls +class ComputeContact : public ComputeInteraction { + + // Virtual implementation +public: + //! Penalty contact implementation + void compute(System& system) override; + + // Accessors +public: + //! Set penalty + void setPenalty(Real penalty); + + // Members +protected: + Real penalty; +}; + +/* -------------------------------------------------------------------------- */ +#endif //__COMPUTE_CONTACT__HH__ diff --git a/Homework3/src/compute_energy.cc b/Homework3/src/compute_energy.cc new file mode 100644 index 0000000..65290c3 --- /dev/null +++ b/Homework3/src/compute_energy.cc @@ -0,0 +1,2 @@ +#include "compute_energy.hh" +/* -------------------------------------------------------------------------- */ diff --git a/Homework3/src/compute_energy.hh b/Homework3/src/compute_energy.hh new file mode 100644 index 0000000..eca44ea --- /dev/null +++ b/Homework3/src/compute_energy.hh @@ -0,0 +1,19 @@ +#ifndef __COMPUTE_ENERGY__HH__ +#define __COMPUTE_ENERGY__HH__ + +/* -------------------------------------------------------------------------- */ +#include "compute.hh" + +//! Base class for energy computation +class ComputeEnergy : public Compute { + + // Methods +public: + Real getValue() { return value; } + +protected: + Real value; +}; + +/* -------------------------------------------------------------------------- */ +#endif //__COMPUTE_ENERGY__HH__ diff --git a/Homework3/src/compute_gravity.cc b/Homework3/src/compute_gravity.cc new file mode 100644 index 0000000..feaaa00 --- /dev/null +++ b/Homework3/src/compute_gravity.cc @@ -0,0 +1,5 @@ +#include "compute_gravity.hh" +#include +/* -------------------------------------------------------------------------- */ +void ComputeGravity::compute(System& system) { +} diff --git a/Homework3/src/compute_gravity.hh b/Homework3/src/compute_gravity.hh new file mode 100644 index 0000000..77071bd --- /dev/null +++ b/Homework3/src/compute_gravity.hh @@ -0,0 +1,27 @@ +#ifndef __COMPUTE_GRAVITY__HH__ +#define __COMPUTE_GRAVITY__HH__ + +/* -------------------------------------------------------------------------- */ +#include "compute_interaction.hh" + +//! Compute Newton gravity interaction +class ComputeGravity : public ComputeInteraction { + + // Virtual implementation +public: + //! Newton gravity implementation + void compute(System& system) override; + + // Accessors +public: + //! set the gravitational constant + void setG(Real G); + + // Members +private: + //! newton constant + Real G = 1.; +}; + +/* -------------------------------------------------------------------------- */ +#endif //__COMPUTE_GRAVITY__HH__ diff --git a/Homework3/src/compute_interaction.cc b/Homework3/src/compute_interaction.cc new file mode 100644 index 0000000..d85c12e --- /dev/null +++ b/Homework3/src/compute_interaction.cc @@ -0,0 +1,3 @@ +#include "compute_interaction.hh" +#include +/* -------------------------------------------------------------------------- */ diff --git a/Homework3/src/compute_interaction.hh b/Homework3/src/compute_interaction.hh new file mode 100644 index 0000000..4374eb4 --- /dev/null +++ b/Homework3/src/compute_interaction.hh @@ -0,0 +1,12 @@ +#ifndef __COMPUTE_INTERACTION__HH__ +#define __COMPUTE_INTERACTION__HH__ + +/* -------------------------------------------------------------------------- */ +#include "compute.hh" + +//! Base class for interaction computation +class ComputeInteraction : public Compute { +}; + +/* -------------------------------------------------------------------------- */ +#endif //__COMPUTE_INTERACTION__HH__ diff --git a/Homework3/src/compute_kinetic_energy.cc b/Homework3/src/compute_kinetic_energy.cc new file mode 100644 index 0000000..ce515f0 --- /dev/null +++ b/Homework3/src/compute_kinetic_energy.cc @@ -0,0 +1,6 @@ +#include "compute_kinetic_energy.hh" +/* -------------------------------------------------------------------------- */ + +void ComputeKineticEnergy::compute(System& system) {} + +/* -------------------------------------------------------------------------- */ diff --git a/Homework3/src/compute_kinetic_energy.hh b/Homework3/src/compute_kinetic_energy.hh new file mode 100644 index 0000000..7fce63b --- /dev/null +++ b/Homework3/src/compute_kinetic_energy.hh @@ -0,0 +1,14 @@ +#ifndef __COMPUTE_KINETIC_ENERGY__HH__ +#define __COMPUTE_KINETIC_ENERGY__HH__ + +/* -------------------------------------------------------------------------- */ +#include "compute_energy.hh" + +//! Compute kinetic energy of system +class ComputeKineticEnergy : public ComputeEnergy { +public: + void compute(System& system) override; +}; + +/* -------------------------------------------------------------------------- */ +#endif //__COMPUTE_KINETIC_ENERGY__HH__ diff --git a/Homework3/src/compute_potential_energy.cc b/Homework3/src/compute_potential_energy.cc new file mode 100644 index 0000000..1174018 --- /dev/null +++ b/Homework3/src/compute_potential_energy.cc @@ -0,0 +1,9 @@ +#include "compute_potential_energy.hh" +/* -------------------------------------------------------------------------- */ + +ComputePotentialEnergy::ComputePotentialEnergy(ComputeInteraction& cForces) + : cForces(cForces) {} + +/* -------------------------------------------------------------------------- */ + +void ComputePotentialEnergy::compute(System& system) {} diff --git a/Homework3/src/compute_potential_energy.hh b/Homework3/src/compute_potential_energy.hh new file mode 100644 index 0000000..aa197c4 --- /dev/null +++ b/Homework3/src/compute_potential_energy.hh @@ -0,0 +1,35 @@ +#ifndef __COMPUTE_POTENTIAL_ENERGY__HH__ +#define __COMPUTE_POTENTIAL_ENERGY__HH__ + +/* -------------------------------------------------------------------------- */ +#include "compute_energy.hh" +#include "compute_interaction.hh" +/* -------------------------------------------------------------------------- */ + +//! Compute potential energy of system +class ComputePotentialEnergy : public ComputeEnergy { + + /* ------------------------------------------------------------------------ */ + /* Constructors/Destructors */ + /* ------------------------------------------------------------------------ */ + +public: + ComputePotentialEnergy(ComputeInteraction& cForces); + + /* ------------------------------------------------------------------------ */ + /* Methods */ + /* ------------------------------------------------------------------------ */ + +public: + void compute(System& system) override; + + /* ------------------------------------------------------------------------ */ + /* Members */ + /* ------------------------------------------------------------------------ */ + +protected: + ComputeInteraction& cForces; +}; + +/* -------------------------------------------------------------------------- */ +#endif //__COMPUTE_POTENTIAL_ENERGY__HH__ diff --git a/Homework3/src/compute_verlet_integration.cc b/Homework3/src/compute_verlet_integration.cc new file mode 100644 index 0000000..829b361 --- /dev/null +++ b/Homework3/src/compute_verlet_integration.cc @@ -0,0 +1,19 @@ +#include "compute_verlet_integration.hh" + +ComputeVerletIntegration::ComputeVerletIntegration(Real dt) : dt(dt) {} + +/* -------------------------------------------------------------------------- */ + +void ComputeVerletIntegration::setDeltaT(Real dt) { +} + +/* -------------------------------------------------------------------------- */ + +void ComputeVerletIntegration::compute(System& system) { +} + +/* -------------------------------------------------------------------------- */ + +void ComputeVerletIntegration::addInteraction( + std::shared_ptr interaction) { +} diff --git a/Homework3/src/compute_verlet_integration.hh b/Homework3/src/compute_verlet_integration.hh new file mode 100644 index 0000000..e64876e --- /dev/null +++ b/Homework3/src/compute_verlet_integration.hh @@ -0,0 +1,33 @@ +#ifndef __COMPUTE_VERLET_INTEGRATION__HH__ +#define __COMPUTE_VERLET_INTEGRATION__HH__ + +/* -------------------------------------------------------------------------- */ +#include "compute.hh" +#include "compute_interaction.hh" +/* -------------------------------------------------------------------------- */ +#include + +//! Integrate equation of motion +class ComputeVerletIntegration : public Compute { + using InteractionList = std::vector>; + + // Constructors/Destructors +public: + ComputeVerletIntegration(Real timestep); + + // Methods +public: + //! Set time step + void setDeltaT(Real dt); + //! Evolve positions and velocities + void compute(System& system) override; + //! Add an interaction to the computation of forces + void addInteraction(std::shared_ptr interaction); + +private: + Real dt; + InteractionList interactions; +}; + +/* -------------------------------------------------------------------------- */ +#endif //__COMPUTE_VERLET_INTEGRATION__HH__ diff --git a/Homework3/src/csv_reader.cc b/Homework3/src/csv_reader.cc new file mode 100644 index 0000000..fa6c397 --- /dev/null +++ b/Homework3/src/csv_reader.cc @@ -0,0 +1,14 @@ +#include "csv_reader.hh" +#include "particles_factory_interface.hh" +#include +#include +/* -------------------------------------------------------------------------- */ +CsvReader::CsvReader(const std::string& filename) : filename(filename) {} +/* -------------------------------------------------------------------------- */ +void CsvReader::read(System& system) { this->compute(system); } +/* -------------------------------------------------------------------------- */ + +void CsvReader::compute(System& system) { +} + +/* -------------------------------------------------------------------------- */ diff --git a/Homework3/src/csv_reader.hh b/Homework3/src/csv_reader.hh new file mode 100644 index 0000000..369c6bf --- /dev/null +++ b/Homework3/src/csv_reader.hh @@ -0,0 +1,32 @@ +#ifndef __CSV_READER__HH__ +#define __CSV_READER__HH__ + +/* -------------------------------------------------------------------------- */ +#include "compute.hh" +#include + +//! Read system from csv input file +class CsvReader : public Compute { + + /* ------------------------------------------------------------------------ */ + /* Constructors/Destructors */ + /* ------------------------------------------------------------------------ */ +public: + //! Construct from filename + CsvReader(const std::string& filename); + + /* ------------------------------------------------------------------------ */ + /* Methods */ + /* ------------------------------------------------------------------------ */ +public: + //! Left here for convenience (calls compute) + void read(System& system); + //! Read input file into system + void compute(System& system); + +protected: + std::string filename; +}; + +/* -------------------------------------------------------------------------- */ +#endif //__CSV_READER__HH__ diff --git a/Homework3/src/csv_writer.cc b/Homework3/src/csv_writer.cc new file mode 100644 index 0000000..55566a8 --- /dev/null +++ b/Homework3/src/csv_writer.cc @@ -0,0 +1,13 @@ +#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) { +} diff --git a/Homework3/src/csv_writer.hh b/Homework3/src/csv_writer.hh new file mode 100644 index 0000000..0fabca4 --- /dev/null +++ b/Homework3/src/csv_writer.hh @@ -0,0 +1,27 @@ +#ifndef __CSV_WRITER__HH__ +#define __CSV_WRITER__HH__ + +/* -------------------------------------------------------------------------- */ +#include "compute.hh" +#include + +//! Write system state to csv file +class CsvWriter : public Compute { + // Constructors/Destructors +public: + //! Construct from filename + CsvWriter(const std::string& filename); + + // Methods +public: + //! Write file (calls compute) + void write(System& system); + //! Write file + void compute(System& system); + +protected: + std::string filename; +}; + +/* -------------------------------------------------------------------------- */ +#endif //__CSV_WRITER__HH__ diff --git a/Homework3/src/main.cc b/Homework3/src/main.cc new file mode 100644 index 0000000..6ab4d05 --- /dev/null +++ b/Homework3/src/main.cc @@ -0,0 +1,25 @@ +#include "compute_gravity.hh" +#include "compute_verlet_integration.hh" +#include "csv_reader.hh" +#include "csv_writer.hh" +#include "my_types.hh" +#include "ping_pong_balls_factory.hh" +#include "planets_factory.hh" +#include "system.hh" +/* -------------------------------------------------------------------------- */ +#include +#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); + } + + return EXIT_SUCCESS; +} diff --git a/Homework3/src/my_types.hh b/Homework3/src/my_types.hh new file mode 100644 index 0000000..8f7ecf8 --- /dev/null +++ b/Homework3/src/my_types.hh @@ -0,0 +1,27 @@ +#ifndef __MY_TYPES_HH__ +#define __MY_TYPES_HH__ + +/* -------------------------------------------------------------------------- */ +#include + +typedef unsigned int UInt; +typedef double Real; + +/* -------------------------------------------------------------------------- */ +#define TO_IMPLEMENT \ + { \ + std::cerr << std::endl \ + << std::endl \ + << "*********************************************************\n" \ + << "To be filled @ " << __FILE__ << ":" << __LINE__ << std::endl \ + << "*********************************************************\n" \ + << std::endl \ + << std::endl; \ + throw; \ + } +/* -------------------------------------------------------------------------- */ + +#define EXERCISE_BEGIN_CORRECTION +#define EXERCISE_END_CORRECTION + +#endif /* __MY_TYPES_HH__ */ diff --git a/Homework3/src/particle.cc b/Homework3/src/particle.cc new file mode 100644 index 0000000..229cbab --- /dev/null +++ b/Homework3/src/particle.cc @@ -0,0 +1,9 @@ +#include "particle.hh" + +void Particle::printself(std::ostream& stream) const { +} + +/* -------------------------------------------------------------------------- */ + +void Particle::initself(std::istream& sstr) { +} diff --git a/Homework3/src/particle.classes b/Homework3/src/particle.classes new file mode 100644 index 0000000..3d66ba1 --- /dev/null +++ b/Homework3/src/particle.classes @@ -0,0 +1,99 @@ +class Real(double) +class UInt(unsigned int) + +class Vector +public Vector(UInt dim); +public ~Vector(); +public Real & operator[](UInt i); +private Real * values; +private UInt dim; +public Real & operator[](UInt i); +public const Real & operator [](UInt dim) const; +public Vector operator -(const Vector & q) const; +public Vector & operator -=(const Vector & q); +public Vector operator +(const Vector & q) const; +public Vector & operator +=(const Vector & q); +public Vector operator *(const Vector & q) const; +public Vector operator /(const Vector & q) const; +public Vector operator *(const Real & s) const; +public Vector operator /(const Real & s) const; +public Vector & operator =(const Real & s); +public Real squaredNorm() const; + + +class Particle +public Particle(); +public ~Particle(); +public Real & getMass(); +public Vector & getPosition(); +public Vector & getVelocity(); +public Vector & getForce(); + +protected Real mass; +protected Vector position; +protected Vector velocity; +protected Vector force; + +class PingPongBall(Particle) +public PingPongBall(); +public ~PingPongBall(); +public Real & getRadius(); +public Real & getContactDissipation(); +private Real radius; +private Real contact_dissipation; + +class Planet(Particle) +public Planet(); +public ~Planet(); +public std::string & getName(); +private std::string name; + +class Compute +public Compute(); +public ~Compute(); +public void compute(System & system)=0; + +class ComputeContact(Compute) +public ComputeContact(); +public ~ComputeContact(); +public void compute(System & system); + +class ComputeGravity(Compute) +public ComputeGravity(); +public ~ComputeGravity(); +public void compute(System & system); + +class ComputeBoundary(Compute) +public ComputeBoundary(); +public ComputeBoundary(const Vector & xmin, const Vector & xmax); +public ~ComputeBoundary(); +public void compute(System & system); +public void setRange((const Vector & xmin, const Vector & xmax); +protected Vector xmin +protected Vector xmax + + +class ComputeVerletIntegration(Compute) +public ComputeVerletIntegration(Real dt); +public ~ComputeVerletIntegration(); +public void compute(System & system); +public setDeltaT(Real dt); +private Real dt; + +class System +public System() +public ~System() +public UInt getListSize(); +public Particle & getParticle(UInt i); +public addParticle(Particle & new_particle); +public removeParticle(UInt particle); +private ListParticles list_particles; + +class SystemEvolution +public SystemEvolution(System & system); +public void evolve(); +public void addcompute(Compute & compute); +protected std::vector computes; +protected System & system; + +class ListParticles(std::vector) \ No newline at end of file diff --git a/Homework3/src/particle.hh b/Homework3/src/particle.hh new file mode 100644 index 0000000..f4bb58c --- /dev/null +++ b/Homework3/src/particle.hh @@ -0,0 +1,44 @@ +#ifndef __PARTICLE__HH__ +#define __PARTICLE__HH__ + +/* -------------------------------------------------------------------------- */ +#include "vector.hh" +/* -------------------------------------------------------------------------- */ + +//! Particle base class +class Particle { + /* ------------------------------------------------------------------------ */ + /* Constructors/Destructors */ + /* ------------------------------------------------------------------------ */ +public: + //! Default destructor (virtual because of virtual functions in this class) + virtual ~Particle() = default; + + // Accessors +public: + //! Get mass + Real& getMass() { return mass; } + //! Get position + Vector& getPosition() { return position; } + //! Get force + Vector& getForce() { return force; } + //! Get velocity + Vector& getVelocity() { return velocity; } + + // I/O functions +public: + /// function to print the content of the class + virtual void printself(std::ostream& stream) const; + + /// function to initilize values from input files + virtual void initself(std::istream& sstr); + + // Members +protected: + Real mass; + Vector position, force, velocity; +}; + + +/* -------------------------------------------------------------------------- */ +#endif //__PARTICLE__HH__ diff --git a/Homework3/src/particles.classes b/Homework3/src/particles.classes new file mode 100644 index 0000000..44e1fb4 --- /dev/null +++ b/Homework3/src/particles.classes @@ -0,0 +1,130 @@ +class CsvReader(Compute) + public CsvReader(std::string const & filename); + public void read(System & system); + public virtual void compute(System & system); + protected std::string filename; + +class ComputeKineticEnergy(ComputeEnergy) + public ComputeKineticEnergy(); + public virtual void compute(); + +class ParticlesFactoryInterface + protected ParticlesFactoryInterface(); + public pure virtual SystemEvolution & createSimulation(Real timestep, std::string const & fname); + public pure virtual Particle & createParticle(); + public static ParticlesFactoryInterface & getInstance(); + protected std::vector list_particles; + protected SystemEvolution * system_evolution; + protected ParticlesFactoryInterface * factory; + +class Compute + public Compute(); + public pure virtual void compute(System & system); + +class ComputeVerletIntegration(Compute) + public ComputeVerletIntegration(Real dt); + public void setDeltaT(Real dt); + public virtual void compute(System & system); + private Real dt; + +class Particle + public Particle(); + public virtual void initself(std::istream & sstr); + public Vector & getVelocity(); + public virtual void printself(std::ostream & stream) const; + public Real & getMass(); + public Vector & getForce(); + public Vector & getPosition(); + protected Vector position; + protected Vector force; + protected Real mass; + protected Vector velocity; + +class SystemEvolution + public SystemEvolution(System & system); + public void evolve(); + public void setNSteps(UInt nsteps); + public void setDumpFreq(UInt nsteps); + public System & getSystem(); + public void addCompute(Compute & compute); + protected UInt freq; + protected std::vector computes; + protected System & system; + protected UInt nsteps; + +class ComputeContact(Compute) + public ComputeContact(); + public virtual void compute(System & system); + public void setPenalty(Real penalty); + protected Real penalty; + +class System + public System(); + public void removeParticle(UInt particle); + public Particle & getParticle(UInt i); + public void addParticle(Particle & new_particle); + public UInt getListSize(); + private std::vector list_particles; + +class ComputePotentialEnergy(ComputeEnergy) + public ComputePotentialEnergy(Compute & cForces); + public virtual void compute(); + protected Compute & cForces; + +class Planet(Particle) + public Planet(); + public std::string & getName(); + private std::string name; + +class Vector + public Vector(); + public Real squaredNorm() const; + public virtual void initself(std::istream & sstr); + public virtual void printself(std::ostream & stream) const; + private UInt const dim; + private Real[3] values; + +class PingPongBallsFactory(ParticlesFactoryInterface) + private PingPongBallsFactory(); + public virtual SystemEvolution & createSimulation(Real timestep, std::string const & fname); + public virtual Particle & createParticle(); + public static ParticlesFactoryInterface & getInstance(); + +class ComputeGravity(Compute) + public ComputeGravity(); + public void setG(Real G); + public virtual void compute(System & system); + private Real G; + +class PlanetsFactory(ParticlesFactoryInterface) + private PlanetsFactory(); + public virtual SystemEvolution & createSimulation(Real timestep, std::string const & fname); + public virtual Particle & createParticle(); + public static ParticlesFactoryInterface & getInstance(); + +class CsvWriter(Compute) + public CsvWriter(std::string const & filename); + public void write(System & system); + public virtual void compute(System & system); + protected std::string filename; + +class PingPongBall(Particle) + public PingPongBall(); + public virtual void printself(std::ostream & stream) const; + public virtual void initself(std::istream & sstr); + public Real & getContactDissipation(); + public Real & getRadius(); + private Real radius; + private Real contact_dissipation; + +class ComputeEnergy(Compute) + public ComputeEnergy(); + public virtual Real getValue(); + protected Real value; + +class ComputeBoundary(Compute) + public ComputeBoundary(); + public virtual void compute(System & system); + protected Vector xmin; + protected Vector xmax; + diff --git a/Homework3/src/particles_factory_interface.cc b/Homework3/src/particles_factory_interface.cc new file mode 100644 index 0000000..2856817 --- /dev/null +++ b/Homework3/src/particles_factory_interface.cc @@ -0,0 +1,8 @@ +#include "particles_factory_interface.hh" +#include "planets_factory.hh" +/* -------------------------------------------------------------------------- */ +ParticlesFactoryInterface& ParticlesFactoryInterface::getInstance() { +} + +/* -------------------------------------------------------------------------- */ +ParticlesFactoryInterface* ParticlesFactoryInterface::factory = nullptr; diff --git a/Homework3/src/particles_factory_interface.hh b/Homework3/src/particles_factory_interface.hh new file mode 100644 index 0000000..b04c38b --- /dev/null +++ b/Homework3/src/particles_factory_interface.hh @@ -0,0 +1,39 @@ +#ifndef __PARTICLES_FACTORY_INTERFACE__HH__ +#define __PARTICLES_FACTORY_INTERFACE__HH__ + +/* -------------------------------------------------------------------------- */ +#include "system_evolution.hh" +/* -------------------------------------------------------------------------- */ + +//! Abstract factory defining interface +class ParticlesFactoryInterface { + // Constructors/Destructors +protected: + //! Instance constructor (protected) + ParticlesFactoryInterface() = default; + +public: + virtual ~ParticlesFactoryInterface() = default; + + // Methods +public: + //! Create a whole simulation from file + virtual SystemEvolution& createSimulation(const std::string& fname, + Real timestep) = 0; + //! Create a new particle + virtual std::unique_ptr createParticle() = 0; + + //! Get singleton instance + static ParticlesFactoryInterface& getInstance(); + + // Members +protected: + std::vector list_particles; + std::unique_ptr system_evolution = nullptr; + + // Standard pointer because constructor is protected (cannot use make_unique) + static ParticlesFactoryInterface* factory; +}; + +/* -------------------------------------------------------------------------- */ +#endif //__PARTICLES_FACTORY_INTERFACE__HH__ diff --git a/Homework3/src/ping_pong_ball.cc b/Homework3/src/ping_pong_ball.cc new file mode 100644 index 0000000..60926ea --- /dev/null +++ b/Homework3/src/ping_pong_ball.cc @@ -0,0 +1,10 @@ +#include "ping_pong_ball.hh" + +/* -------------------------------------------------------------------------- */ +void PingPongBall::printself(std::ostream& stream) const { +} + +/* -------------------------------------------------------------------------- */ + +void PingPongBall::initself(std::istream& sstr) { +} diff --git a/Homework3/src/ping_pong_ball.hh b/Homework3/src/ping_pong_ball.hh new file mode 100644 index 0000000..6c5169f --- /dev/null +++ b/Homework3/src/ping_pong_ball.hh @@ -0,0 +1,27 @@ +#ifndef __PING_PONG_BALL__HH__ +#define __PING_PONG_BALL__HH__ + +/* -------------------------------------------------------------------------- */ +#include "particle.hh" + +//! Class for ping-pong ball +class PingPongBall : public Particle { + /* ------------------------------------------------------------------------ */ + /* Methods */ + /* ------------------------------------------------------------------------ */ + +public: + //! Get contact dissipation + Real& getContactDissipation() { return contact_dissipation; } + //! Get ball radius + Real& getRadius() { return radius; } + + void printself(std::ostream& stream) const override; + void initself(std::istream& sstr) override; + +private: + Real radius, contact_dissipation; +}; + +/* -------------------------------------------------------------------------- */ +#endif //__PING_PONG_BALL__HH__ diff --git a/Homework3/src/ping_pong_balls_factory.cc b/Homework3/src/ping_pong_balls_factory.cc new file mode 100644 index 0000000..9a43b06 --- /dev/null +++ b/Homework3/src/ping_pong_balls_factory.cc @@ -0,0 +1,26 @@ +#include "ping_pong_balls_factory.hh" +#include "compute_contact.hh" +#include "compute_verlet_integration.hh" +#include "csv_reader.hh" +#include "csv_writer.hh" +#include "ping_pong_ball.hh" +#include +#include +/* -------------------------------------------------------------------------- */ + +std::unique_ptr PingPongBallsFactory::createParticle() { +} + +/* -------------------------------------------------------------------------- */ + +SystemEvolution& +PingPongBallsFactory::createSimulation(const std::string& fname, + Real timestep) { +} + +/* -------------------------------------------------------------------------- */ + +ParticlesFactoryInterface& PingPongBallsFactory::getInstance() { +} + +/* -------------------------------------------------------------------------- */ diff --git a/Homework3/src/ping_pong_balls_factory.hh b/Homework3/src/ping_pong_balls_factory.hh new file mode 100644 index 0000000..5a75091 --- /dev/null +++ b/Homework3/src/ping_pong_balls_factory.hh @@ -0,0 +1,30 @@ +#ifndef __PING_PING_BALLS_FACTORY__HH__ +#define __PING_PING_BALLS_FACTORY__HH__ + +/* -------------------------------------------------------------------------- */ +#include "particles_factory_interface.hh" +#include "ping_pong_ball.hh" +/* -------------------------------------------------------------------------- */ + +//! Factory for ping-pong balls +class PingPongBallsFactory : public ParticlesFactoryInterface { + /* ------------------------------------------------------------------------ */ + /* Constructors/Destructors */ + /* ------------------------------------------------------------------------ */ +private: + PingPongBallsFactory() = default; + + /* ------------------------------------------------------------------------ */ + /* Methods */ + /* ------------------------------------------------------------------------ */ +public: + SystemEvolution& createSimulation(const std::string& fname, + Real timestep) override; + + std::unique_ptr createParticle() override; + + static ParticlesFactoryInterface& getInstance(); +}; + +/* -------------------------------------------------------------------------- */ +#endif //__PING_PING_BALLS_FACTORY__HH__ diff --git a/Homework3/src/planet.cc b/Homework3/src/planet.cc new file mode 100644 index 0000000..e841202 --- /dev/null +++ b/Homework3/src/planet.cc @@ -0,0 +1,9 @@ +#include "planet.hh" + +void Planet::initself(std::istream &stream) { +} + +/* -------------------------------------------------------------------------- */ + +void Planet::printself(std::ostream &stream) const { +} diff --git a/Homework3/src/planet.hh b/Homework3/src/planet.hh new file mode 100644 index 0000000..0ebf4c1 --- /dev/null +++ b/Homework3/src/planet.hh @@ -0,0 +1,27 @@ +#ifndef __PLANET__HH__ +#define __PLANET__HH__ + +/* -------------------------------------------------------------------------- */ +#include "particle.hh" +/* -------------------------------------------------------------------------- */ +#include +/* -------------------------------------------------------------------------- */ + +//! Class for planet +class Planet : public Particle { + // Methods +public: + //! Get name + std::string& getName() { return name; } + + // Inherited methods +public: + void initself(std::istream& stream) override; + void printself(std::ostream& stream) const override; + +private: + std::string name; +}; + +/* -------------------------------------------------------------------------- */ +#endif //__PLANET__HH__ diff --git a/Homework3/src/planets_factory.cc b/Homework3/src/planets_factory.cc new file mode 100644 index 0000000..a29e83b --- /dev/null +++ b/Homework3/src/planets_factory.cc @@ -0,0 +1,23 @@ +#include "planets_factory.hh" +#include "compute_gravity.hh" +#include "compute_verlet_integration.hh" +#include "csv_reader.hh" +#include "csv_writer.hh" +#include "planet.hh" +#include +/* -------------------------------------------------------------------------- */ + +std::unique_ptr PlanetsFactory::createParticle() { +} + +/* -------------------------------------------------------------------------- */ + +SystemEvolution& PlanetsFactory::createSimulation(const std::string& fname, + Real timestep) { +} + +/* -------------------------------------------------------------------------- */ + +ParticlesFactoryInterface& PlanetsFactory::getInstance() { +} +/* -------------------------------------------------------------------------- */ diff --git a/Homework3/src/planets_factory.hh b/Homework3/src/planets_factory.hh new file mode 100644 index 0000000..861ef60 --- /dev/null +++ b/Homework3/src/planets_factory.hh @@ -0,0 +1,31 @@ +#ifndef __PLANETS_FACTORY__HH__ +#define __PLANETS_FACTORY__HH__ + +/* -------------------------------------------------------------------------- */ +#include "particles_factory_interface.hh" +#include "planet.hh" +/* -------------------------------------------------------------------------- */ + +//! Factory for planet simulations +class PlanetsFactory : public ParticlesFactoryInterface { + /* ------------------------------------------------------------------------ */ + /* Constructors/Destructors */ + /* ------------------------------------------------------------------------ */ +private: + PlanetsFactory() = default; + + /* ------------------------------------------------------------------------ */ + /* Methods */ + /* ------------------------------------------------------------------------ */ + +public: + SystemEvolution& createSimulation(const std::string& fname, + Real timestep) override; + + std::unique_ptr createParticle() override; + + static ParticlesFactoryInterface& getInstance(); +}; + +/* -------------------------------------------------------------------------- */ +#endif //__PLANETS_FACTORY__HH__ diff --git a/Homework3/src/system.cc b/Homework3/src/system.cc new file mode 100644 index 0000000..d136a32 --- /dev/null +++ b/Homework3/src/system.cc @@ -0,0 +1,14 @@ +#include "system.hh" + +Particle& System::getParticle(UInt i) { +} + +/* -------------------------------------------------------------------------- */ + +void System::addParticle(const std::shared_ptr& new_particle) { +} + +/* -------------------------------------------------------------------------- */ + +UInt System::getNbParticles() { +} diff --git a/Homework3/src/system.hh b/Homework3/src/system.hh new file mode 100644 index 0000000..8dc5c12 --- /dev/null +++ b/Homework3/src/system.hh @@ -0,0 +1,37 @@ +#ifndef __SYSTEM__HH__ +#define __SYSTEM__HH__ + +/* -------------------------------------------------------------------------- */ +#include "my_types.hh" +#include "particle.hh" +#include +#include +/* -------------------------------------------------------------------------- */ + +//! Container for particles +class System { + /* + * No need for constructor/destructor with std::unique_ptr + */ + + // List of particle pointers + using ParticleList = std::vector>; + + // 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& new_particle); + //! Get number of particles + UInt getNbParticles(); + + +private: + ParticleList list_particles; +}; + +/* -------------------------------------------------------------------------- */ +#endif //__SYSTEM__HH__ diff --git a/Homework3/src/system_evolution.cc b/Homework3/src/system_evolution.cc new file mode 100644 index 0000000..dcc7eab --- /dev/null +++ b/Homework3/src/system_evolution.cc @@ -0,0 +1,28 @@ +#include "system_evolution.hh" +#include "csv_writer.hh" +/* -------------------------------------------------------------------------- */ +#include +#include +/* -------------------------------------------------------------------------- */ + +SystemEvolution::SystemEvolution(std::unique_ptr system) + : system(std::move(system)) {} + +/* -------------------------------------------------------------------------- */ + +void SystemEvolution::evolve() { +} + +/* -------------------------------------------------------------------------- */ + +void SystemEvolution::addCompute(const std::shared_ptr& 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; } +/* -------------------------------------------------------------------------- */ diff --git a/Homework3/src/system_evolution.hh b/Homework3/src/system_evolution.hh new file mode 100644 index 0000000..3b33052 --- /dev/null +++ b/Homework3/src/system_evolution.hh @@ -0,0 +1,42 @@ +#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); + + /* ------------------------------------------------------------------------ */ + /* Methods */ + /* ------------------------------------------------------------------------ */ +public: + //! Evolve all time steps + void evolve(); + //! Add compute to list of computes + void addCompute(const std::shared_ptr& compute); + //! Get the system object + System& getSystem(); + + void setNSteps(UInt nsteps); + void setDumpFreq(UInt freq); + + /* ------------------------------------------------------------------------ */ + /* Members */ + /* ------------------------------------------------------------------------ */ +protected: + std::vector> computes; + std::unique_ptr system; + UInt nsteps, freq; +}; + +/* -------------------------------------------------------------------------- */ +#endif //__SYSTEM_EVOLUTION__HH__ diff --git a/Homework3/src/vector.cc b/Homework3/src/vector.cc new file mode 100644 index 0000000..e61ed63 --- /dev/null +++ b/Homework3/src/vector.cc @@ -0,0 +1,68 @@ +#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) { + return stream; +} + +/* -------------------------------------------------------------------------- */ +/// standard input stream operator +std::istream& operator>>(std::istream& stream, Vector& _this) { + return stream; +} diff --git a/Homework3/src/vector.hh b/Homework3/src/vector.hh new file mode 100644 index 0000000..1deb5b9 --- /dev/null +++ b/Homework3/src/vector.hh @@ -0,0 +1,72 @@ +#ifndef __VECTOR__HH__ +#define __VECTOR__HH__ + +/* -------------------------------------------------------------------------- */ +#include "my_types.hh" +/* -------------------------------------------------------------------------- */ +#include + +/** + * @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 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__