Molecular Dynamics Simulation  1.0
Public Member Functions | Private Attributes | List of all members
DirectSumContainer Class Reference

Wrapper class for a set of particles. More...

#include <DirectSumContainer.h>

Inheritance diagram for DirectSumContainer:
Inheritance graph
Collaboration diagram for DirectSumContainer:
Collaboration graph

Public Member Functions

 DirectSumContainer ()=default
 Default constructor. More...
 
 DirectSumContainer (int n)
 Constructor with amount of particles. More...
 
void reserve (size_t n) override
 Reserves memory for particles. More...
 
std::size_t size () const override
 Returns the amount of particles. More...
 
std::size_t capacity () const
 Returns the capacity of the container. More...
 
void addParticle (const Particle &p) override
 Adds a particle to the container. More...
 
void addParticle (Particle &&p) override
 Adds a particle to the container. More...
 
Particleoperator[] (int i) override
 Returns a particle. More...
 
std::vector< Particle >::iterator begin () override
 Returns an iterator to the first particle. More...
 
std::vector< Particle >::iterator end () override
 Returns an end iterator for this container. More...
 
std::vector< Particle >::const_iterator begin () const override
 Returns a const iterator to the first particle. More...
 
std::vector< Particle >::const_iterator end () const override
 Returns a const end iterator for this container. More...
 
const std::vector< Particle > & getParticles () const override
 Returns a vector of all particles in the container. More...
 
void prepareForceCalculation () override
 Prepares everything for the force calculations (must be called before applySimpleForces and applyPairwiseForces) More...
 
void applySimpleForces (const std::vector< std::shared_ptr< SimpleForceSource >> &simple_force_sources) override
 Applies the given simple force sources to the particles. More...
 
void applyPairwiseForces (const std::vector< std::shared_ptr< PairwiseForceSource >> &pairwise_force_sources) override
 Applies the given force sources to the particles. More...
 
void applyTargettedForces (const std::vector< std::shared_ptr< TargettedForceSource >> &targetted_force_sources, double curr_simulation_time) override
 Applies the given targetted force sources to the particles. More...
 
- Public Member Functions inherited from ParticleContainer
virtual ~ParticleContainer ()=default
 Virtual destructor for correct deconstruction of inheriting classes. More...
 

Private Attributes

std::vector< Particleparticles
 Internal data structure for the particles. More...
 

Detailed Description

Wrapper class for a set of particles.

Container class for particles, which provides some convenience methods.

Definition at line 13 of file DirectSumContainer.h.

Constructor & Destructor Documentation

◆ DirectSumContainer() [1/2]

DirectSumContainer::DirectSumContainer ( )
default

Default constructor.

Generates an empty DirectSumContainer object. If the amount of particles is known, it is recommended to use the constructor with the amount of particles as parameter.

◆ DirectSumContainer() [2/2]

DirectSumContainer::DirectSumContainer ( int  n)
explicit

Constructor with amount of particles.

Parameters
nAmount of particles

Generates an empty DirectSumContainer object and reserves memory for the given amount of particles.

Definition at line 7 of file DirectSumContainer.cpp.

7 { particles.reserve(n); }
std::vector< Particle > particles
Internal data structure for the particles.

Member Function Documentation

◆ addParticle() [1/2]

void DirectSumContainer::addParticle ( const Particle p)
overridevirtual

Adds a particle to the container.

Parameters
pParticle to be added

Adds a particle to the container.

Implements ParticleContainer.

Definition at line 15 of file DirectSumContainer.cpp.

15 { particles.push_back(p); }

◆ addParticle() [2/2]

void DirectSumContainer::addParticle ( Particle &&  p)
overridevirtual

Adds a particle to the container.

Parameters
pParticle to be added

Adds a particle to the container.

Implements ParticleContainer.

Definition at line 17 of file DirectSumContainer.cpp.

17 { particles.push_back(std::move(p)); }

◆ applyPairwiseForces()

void DirectSumContainer::applyPairwiseForces ( const std::vector< std::shared_ptr< PairwiseForceSource >> &  pairwise_force_sources)
overridevirtual

Applies the given force sources to the particles.

Parameters
pairwise_force_sourcesVector of force sources to be applied

Applies the given force sources to the particles in the container. Uses newton's third law to calculate the forces between the particles in a more optimized way.

Implements ParticleContainer.

Definition at line 42 of file DirectSumContainer.cpp.

42  {
43 #ifdef _OPENMP
44 #pragma omp parallel for schedule(static)
45 #endif
46  for (auto it1 = particles.begin(); it1 != particles.end(); ++it1) {
47  std::array<double, 3> total_force{0, 0, 0};
48 
49  for (auto it2 = particles.begin(); it2 != particles.end(); ++it2) {
50  if (it1 == it2) continue;
51  if (it1->isLocked() && it2->isLocked()) continue;
52  for (auto& force : force_sources) {
53  total_force = total_force + force->calculateForce(*it1, *it2);
54  }
55  }
56  it1->setF(it1->getF() + total_force);
57  }
58 }

◆ applySimpleForces()

void DirectSumContainer::applySimpleForces ( const std::vector< std::shared_ptr< SimpleForceSource >> &  simple_force_sources)
overridevirtual

Applies the given simple force sources to the particles.

Parameters
simple_force_sourcesList of simple force sources to be applied

Applies the given simple force sources to the particles in the container.

Implements ParticleContainer.

Definition at line 33 of file DirectSumContainer.cpp.

33  {
34  for (auto& p : particles) {
35  if (p.isLocked()) continue;
36  for (auto& force : simple_force_sources) {
37  p.setF(p.getF() + force->calculateForce(p));
38  }
39  }
40 }

◆ applyTargettedForces()

void DirectSumContainer::applyTargettedForces ( const std::vector< std::shared_ptr< TargettedForceSource >> &  targetted_force_sources,
double  curr_simulation_time 
)
overridevirtual

Applies the given targetted force sources to the particles.

Parameters
targetted_force_sourcesList of targetted force sources to be applied
curr_simulation_timeCurrent simulation time

Applies the given targetted force sources to the particles in the container.

Implements ParticleContainer.

Definition at line 60 of file DirectSumContainer.cpp.

61  {
62  for (const auto& force_source : force_sources) {
63  force_source->applyForce(particles, curr_simulation_time);
64  }
65 }

◆ begin() [1/2]

std::vector< Particle >::const_iterator DirectSumContainer::begin ( ) const
overridevirtual

Returns a const iterator to the first particle.

Returns
Const iterator to the first particle

Returns the begin const iterator for the internal data structure.

Implements ParticleContainer.

Definition at line 25 of file DirectSumContainer.cpp.

25 { return particles.begin(); }

◆ begin() [2/2]

std::vector< Particle >::iterator DirectSumContainer::begin ( )
overridevirtual

Returns an iterator to the first particle.

Returns
Iterator to the first particle

Returns the begin iterator for the internal data structure.

Implements ParticleContainer.

Definition at line 21 of file DirectSumContainer.cpp.

21 { return particles.begin(); }

◆ capacity()

std::size_t DirectSumContainer::capacity ( ) const

Returns the capacity of the container.

Returns
Capacity of the container

Returns the capacity of the container.

Definition at line 13 of file DirectSumContainer.cpp.

13 { return particles.capacity(); }

◆ end() [1/2]

std::vector< Particle >::const_iterator DirectSumContainer::end ( ) const
overridevirtual

Returns a const end iterator for this container.

Returns
Const end iterator for this container

Returns the end const iterator for the internal data structure.

Implements ParticleContainer.

Definition at line 27 of file DirectSumContainer.cpp.

27 { return particles.end(); }

◆ end() [2/2]

std::vector< Particle >::iterator DirectSumContainer::end ( )
overridevirtual

Returns an end iterator for this container.

Returns
End iterator for this container

Returns the end iterator for the internal data structure.

Implements ParticleContainer.

Definition at line 23 of file DirectSumContainer.cpp.

23 { return particles.end(); }

◆ getParticles()

const std::vector< Particle > & DirectSumContainer::getParticles ( ) const
overridevirtual

Returns a vector of all particles in the container.

Returns
Vector of all particles in the container

Implements ParticleContainer.

Definition at line 29 of file DirectSumContainer.cpp.

29 { return particles; }

◆ operator[]()

Particle & DirectSumContainer::operator[] ( int  i)
overridevirtual

Returns a particle.

Parameters
iIndex of the particle
Returns
Particle

Returns the particle at the given index in the internal data structure.

Implements ParticleContainer.

Definition at line 19 of file DirectSumContainer.cpp.

19 { return particles[i]; }

◆ prepareForceCalculation()

void DirectSumContainer::prepareForceCalculation ( )
overridevirtual

Prepares everything for the force calculations (must be called before applySimpleForces and applyPairwiseForces)

Implements ParticleContainer.

Definition at line 31 of file DirectSumContainer.cpp.

31 {}

◆ reserve()

void DirectSumContainer::reserve ( size_t  n)
overridevirtual

Reserves memory for particles.

Parameters
nAmount of particles

Reserves memory for the given amount of particles.

Implements ParticleContainer.

Definition at line 9 of file DirectSumContainer.cpp.

9 { particles.reserve(n); }

◆ size()

std::size_t DirectSumContainer::size ( ) const
overridevirtual

Returns the amount of particles.

Returns
Amount of particles

Returns the amount of particles in the container.

Implements ParticleContainer.

Definition at line 11 of file DirectSumContainer.cpp.

11 { return particles.size(); }

Member Data Documentation

◆ particles

std::vector<Particle> DirectSumContainer::particles
private

Internal data structure for the particles.

Definition at line 17 of file DirectSumContainer.h.


The documentation for this class was generated from the following files: