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

Class to calculate the harmonic force on a particle. More...

#include <HarmonicForce.h>

Inheritance diagram for HarmonicForce:
Inheritance graph
Collaboration diagram for HarmonicForce:
Collaboration graph

Public Member Functions

 HarmonicForce (const std::variant< SimulationParams::DirectSumType, SimulationParams::LinkedCellsType > &container_data)
 Constructor for the GlobalDownwardsGravity class. More...
 
std::array< double, 3UL > calculateForce (Particle &p) const override
 Calculates the harmonic force on a particle supplied by all its connected neighbors. More...
 
 operator std::string () const override
 Returns "HarmonicForce" as string. More...
 
- Public Member Functions inherited from SimpleForceSource
virtual ~SimpleForceSource ()=default
 Virtual destructor for correct cleanup of derived classes. More...
 

Private Attributes

bool is_finite_container
 
std::array< double, 3 > domain_size
 

Detailed Description

Class to calculate the harmonic force on a particle.

Definition at line 12 of file HarmonicForce.h.

Constructor & Destructor Documentation

◆ HarmonicForce()

HarmonicForce::HarmonicForce ( const std::variant< SimulationParams::DirectSumType, SimulationParams::LinkedCellsType > &  container_data)
explicit

Constructor for the GlobalDownwardsGravity class.

Definition at line 6 of file HarmonicForce.cpp.

6  {
7  if (std::holds_alternative<SimulationParams::DirectSumType>(container_data)) {
8  is_finite_container = false;
9  } else {
10  is_finite_container = true;
11  domain_size = std::get<SimulationParams::LinkedCellsType>(container_data).domain_size;
12  }
13 }
std::array< double, 3 > domain_size
Definition: HarmonicForce.h:14
bool is_finite_container
Definition: HarmonicForce.h:13

Member Function Documentation

◆ calculateForce()

std::array< double, 3UL > HarmonicForce::calculateForce ( Particle p) const
overridevirtual

Calculates the harmonic force on a particle supplied by all its connected neighbors.

Parameters
pParticle
Returns
std::array<double, 3UL> Force vector

Implements SimpleForceSource.

Definition at line 15 of file HarmonicForce.cpp.

15  {
16  auto total_harmonic_force = std::array<double, 3UL>{0, 0, 0};
17 
18  for (auto& [ptr_diff, l_0, k] : p.getConnectedParticles()) {
19  Particle* connected_particle = &p + ptr_diff;
20  auto displacement = connected_particle->getX() - p.getX();
21 
22  if (is_finite_container) {
23  for (size_t i = 0; i < displacement.size(); i++) {
24  if (std::abs(displacement[i]) > domain_size[i] * 0.5) {
25  bool is_positive = displacement[i] > 0;
26  if (is_positive) {
27  displacement[i] -= domain_size[i];
28  } else {
29  displacement[i] += domain_size[i];
30  }
31  }
32  }
33  }
34 
35  const double dist = ArrayUtils::L2Norm(displacement);
36 
37  const auto f_harmonic = (k * (dist - l_0) / dist) * displacement;
38 
39  total_harmonic_force = total_harmonic_force + f_harmonic;
40  }
41 
42  return total_harmonic_force;
43 }
Class to represent a particle.
Definition: Particle.h:26
const std::array< double, 3 > & getX() const
Gets the position of the particle.
Definition: Particle.h:157
const std::vector< std::tuple< long, double, double > > & getConnectedParticles() const
Gets the list of connected particles.
Definition: Particle.h:207
auto L2Norm(const Container &c)
Definition: ArrayUtils.h:174

◆ operator std::string()

HarmonicForce::operator std::string ( ) const
explicitoverridevirtual

Returns "HarmonicForce" as string.

Implements SimpleForceSource.

Definition at line 45 of file HarmonicForce.cpp.

45 { return "HarmonicForce"; }

Member Data Documentation

◆ domain_size

std::array<double, 3> HarmonicForce::domain_size
private

Definition at line 14 of file HarmonicForce.h.

◆ is_finite_container

bool HarmonicForce::is_finite_container
private

Definition at line 13 of file HarmonicForce.h.


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