Simpatico  v1.10
Potential.cpp
1 #include "Potential.h"
2 
3 /*
4 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
5 *
6 * Copyright 2010 - 2017, The Regents of the University of Minnesota
7 * Distributed under the terms of the GNU General Public License.
8 */
9 
10 namespace DdMd
11 {
12 
13  using namespace Util;
14 
15  /*
16  * Constructor.
17  */
19  : stress_(),
20  energy_(),
21  reverseUpdateFlag_(false)
22  { setClassName("Potential"); }
23 
24  /*
25  * Destructor.
26  */
28  {}
29 
30  /*
31  * Set flag to identify if reverse communication is enabled.
32  */
34  { reverseUpdateFlag_ = reverseUpdateFlag; }
35 
36  /*
37  * Get the value of the total energy.
38  */
39  double Potential::energy() const
40  { return energy_.value(); }
41 
42  /*
43  * Set a value for the total energy.
44  */
46  { energy_.set(energy); }
47 
48  /*
49  * Mark the energy as unknown.
50  */
52  { energy_.unset(); }
53 
54  /*
55  * Is the energy set?
56  */
58  { return energy_.isSet(); }
59 
60  /*
61  * Get the value for the total stress.
62  */
64  { return stress_.value(); }
65 
66  /*
67  * Set a value for the pressure.
68  */
69  double Potential::pressure() const
70  {
71  double p = 0.0;
72  for (int i = 0; i < Dimension; ++i) {
73  p += stress_.value()(i, i);
74  }
75  p /= 3.0;
76  return p;
77  }
78 
79  /*
80  * Set a value for the total stress.
81  */
83  { stress_.set(stress); }
84 
85  /*
86  * Mark the stress as unknown.
87  */
89  { stress_.unset(); }
90 
91  /*
92  * Is the stress set?
93  */
95  { return stress_.isSet(); }
96 
97  /*
98  * Compute atomic forces and stress on all processors.
99  *
100  * Default implementation just calls computeForces and computeStress.
101  */
102  #ifdef UTIL_MPI
103  void Potential::computeForcesAndStress(MPI::Intracomm& communicator)
104  #else
106  #endif
107  {
108  computeForces();
109  #ifdef UTIL_MPI
110  computeStress(communicator);
111  #else
112  computeForces();
113  #endif
114  }
115 
116  /*
117  * Reduce energy from all processors.
118  */
119  #ifdef UTIL_MPI
120  void Potential::reduceEnergy(double localEnergy, MPI::Intracomm& communicator)
121  #else
122  void Potential::reduceEnergy(double localEnergy)
123  #endif
124  {
125  #ifdef UTIL_MPI
126  double totalEnergy = 0.0;
127  communicator.Reduce(&localEnergy, &totalEnergy, 1,
128  MPI::DOUBLE, MPI::SUM, 0);
129  if (communicator.Get_rank() != 0) {
130  totalEnergy = 0.0;
131  }
132  setEnergy(totalEnergy);
133  #else
134  setEnergy(localEnergy);
135  #endif
136  }
137 
138  /*
139  * Reduce stress from all processors.
140  */
141  #ifdef UTIL_MPI
142  void Potential::reduceStress(Tensor& localStress, MPI::Intracomm& communicator)
143  #else
144  void PairPotential::reduceStress(Tensor& localStress)
145  #endif
146  {
147  #ifdef UTIL_MPI
148  Tensor totalStress;
149  communicator.Reduce(&localStress(0,0), &totalStress(0,0),
150  Dimension*Dimension, MPI::DOUBLE, MPI::SUM, 0);
151  if (communicator.Get_rank() != 0) {
152  totalStress.zero();
153  }
154  setStress(totalStress);
155  #else
156  setStress(localStress);
157  #endif
158  }
159 
160  #ifdef UTIL_MPI
161  /*
162  * Is the potential in a valid internal state?
163  */
164  bool Potential::isValid(MPI::Intracomm& communicator) const
165  {
166  energy_.isValid(communicator);
167  stress_.isValid(communicator);
168  return true;
169  }
170  #endif
171 
172 }
void setStress(const Tensor &stress)
Set a value for the total stress.
Definition: Potential.cpp:82
void unsetStress()
Mark the stress as unknown (nullify).
Definition: Potential.cpp:88
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
bool isStressSet() const
Is the stress set (known)?
Definition: Potential.cpp:94
void reduceStress(Tensor &localStress, MPI::Intracomm &communicator)
Add local stresses from all processors, set total on master.
Definition: Potential.cpp:142
void set(const T &value)
Set the value and mark as set.
Definition: Setable.h:107
bool isSet() const
Is this object set (is the value known)?
Definition: Setable.h:124
virtual void computeForces()=0
Add force contributions to all atomic forces.
Tensor & zero()
Set all elements of this tensor to zero.
Definition: Tensor.h:441
virtual void computeStress(MPI::Intracomm &communicator)
Compute stress on all processors.
Definition: Potential.h:104
Parallel domain decomposition (DD) MD simulation.
A Tensor represents a Cartesian tensor.
Definition: Tensor.h:32
virtual void computeForcesAndStress(MPI::Intracomm &communicator)
Compute forces and stress for all processors.
Definition: Potential.cpp:103
virtual bool isValid(MPI::Intracomm &communicator) const
Is the potential in a valid internal state?
Definition: Potential.cpp:164
void unsetEnergy()
Mark the energy as unknown (nullify).
Definition: Potential.cpp:51
const T & value() const
Return value (if set).
Definition: Setable.h:132
void setReverseUpdateFlag(bool reverseUpdateFlag)
Set flag to identify if reverse communication is enabled.
Definition: Potential.cpp:33
bool isValid(MPI::Intracomm &communicator) const
Test consistency of states on different processors.
Definition: Setable.h:163
Utility classes for scientific computation.
Definition: accumulators.mod:1
double pressure() const
Return the pressure.
Definition: Potential.cpp:69
void unset()
Unset the value (mark as unknown).
Definition: Setable.h:116
bool reverseUpdateFlag() const
Get flag to identify if reverse communication is enabled.
Definition: Potential.h:228
double energy() const
Return the total potential, from all processors.
Definition: Potential.cpp:39
Tensor stress() const
Return the stress tensor.
Definition: Potential.cpp:63
Potential()
Constructor.
Definition: Potential.cpp:18
void reduceEnergy(double localEnergy, MPI::Intracomm &communicator)
Add local energies from all processors, set energy on master.
Definition: Potential.cpp:120
void setClassName(const char *className)
Set class name string.
void setEnergy(double energy)
Set a value for the total energy.
Definition: Potential.cpp:45
virtual ~Potential()
Destructor.
Definition: Potential.cpp:27
bool isEnergySet() const
Is the energy set (known)?
Definition: Potential.cpp:57