Molecular Dynamics Simulation  1.0
DirectSumContainer.cpp
Go to the documentation of this file.
1 #include "DirectSumContainer.h"
2 
3 #include <omp.h>
4 
5 #include "utils/ArrayUtils.h"
6 
8 
9 void DirectSumContainer::reserve(size_t n) { particles.reserve(n); }
10 
11 std::size_t DirectSumContainer::size() const { return particles.size(); }
12 
13 std::size_t DirectSumContainer::capacity() const { return particles.capacity(); }
14 
15 void DirectSumContainer::addParticle(const Particle& p) { particles.push_back(p); }
16 
17 void DirectSumContainer::addParticle(Particle&& p) { particles.push_back(std::move(p)); }
18 
20 
21 std::vector<Particle>::iterator DirectSumContainer::begin() { return particles.begin(); }
22 
23 std::vector<Particle>::iterator DirectSumContainer::end() { return particles.end(); }
24 
25 std::vector<Particle>::const_iterator DirectSumContainer::begin() const { return particles.begin(); }
26 
27 std::vector<Particle>::const_iterator DirectSumContainer::end() const { return particles.end(); }
28 
29 const std::vector<Particle>& DirectSumContainer::getParticles() const { return particles; }
30 
32 
33 void DirectSumContainer::applySimpleForces(const std::vector<std::shared_ptr<SimpleForceSource>>& simple_force_sources) {
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 }
41 
42 void DirectSumContainer::applyPairwiseForces(const std::vector<std::shared_ptr<PairwiseForceSource>>& force_sources) {
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 }
59 
60 void DirectSumContainer::applyTargettedForces(const std::vector<std::shared_ptr<TargettedForceSource>>& force_sources,
61  double curr_simulation_time) {
62  for (const auto& force_source : force_sources) {
63  force_source->applyForce(particles, curr_simulation_time);
64  }
65 }
Particle & operator[](int i) override
Returns a particle.
std::size_t capacity() const
Returns the capacity of the container.
std::vector< Particle >::iterator begin() override
Returns an iterator to the first particle.
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.
std::vector< Particle >::iterator end() override
Returns an end iterator for this container.
DirectSumContainer()=default
Default constructor.
void reserve(size_t n) override
Reserves memory for particles.
void applySimpleForces(const std::vector< std::shared_ptr< SimpleForceSource >> &simple_force_sources) override
Applies the given simple force sources to the particles.
std::vector< Particle > particles
Internal data structure for the particles.
void addParticle(const Particle &p) override
Adds a particle to the container.
void applyPairwiseForces(const std::vector< std::shared_ptr< PairwiseForceSource >> &pairwise_force_sources) override
Applies the given force sources to the particles.
void prepareForceCalculation() override
Prepares everything for the force calculations (must be called before applySimpleForces and applyPair...
std::size_t size() const override
Returns the amount of particles.
const std::vector< Particle > & getParticles() const override
Returns a vector of all particles in the container.
Class to represent a particle.
Definition: Particle.h:26