Simpatico  v1.10
StressCalculator.cpp
1 /*
2 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
3 *
4 * Copyright 2010 - 2017, The Regents of the University of Minnesota
5 * Distributed under the terms of the GNU General Public License.
6 */
7 
8 #include "StressCalculator.h"
9 #include <util/space/Vector.h>
10 
11 namespace McMd
12 {
13 
14  using namespace Util;
15 
16  /*
17  * Constructor (protected).
18  */
20  : createsStress_(createsStress)
21  { stress_.unset(); }
22 
23  /*
24  * Return true iff subclass is a potential energy that creates stress.
25  */
27  { return createsStress_; }
28 
29  /*
30  * Mark the stress as unknown.
31  */
33  { stress_.unset(); }
34 
35  /*
36  * Get the nonbonded stress tensor.
37  */
39  {
40  UTIL_CHECK(createsStress_);
41 
42  // If necessary, compute stress tensor
43  if (!stress_.isSet()) {
44  computeStress();
45  }
46 
47  // Get full stress tensor
48  stress = stress_.value();
49  }
50 
51  /*
52  * Get the nonbonded x, y, z pressures
53  */
55  {
56  UTIL_CHECK(createsStress_);
57 
58  // If necessary, compute stress tensor
59  if (!stress_.isSet()) {
60  computeStress();
61  }
62 
63  // Get diagonal components of stress tensor (pressures)
64  for (int i=0; i < Dimension; ++i) {
65  pressures[i] = stress_.value()(i, i);
66  }
67  }
68 
69  /*
70  * Get the isotropic pressure = Tr(stress)/3
71  */
72  void StressCalculator::computeStress(double& pressure)
73  {
74  UTIL_CHECK(createsStress_);
75 
76  // If necessary, compute stress tensor
77  if (!stress_.isSet()) {
78  computeStress();
79  }
80 
81  // Get pressure = average of diagonal components.
82  pressure = 0.0;
83  for (int i=0; i < Dimension; ++i) {
84  pressure += stress_.value()(i, i);
85  }
86  pressure = pressure/double(Dimension);
87  }
88 
89 }
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
A Vector is a Cartesian vector.
Definition: Vector.h:75
A Tensor represents a Cartesian tensor.
Definition: Tensor.h:32
Utility classes for scientific computation.
Definition: accumulators.mod:1
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition: global.h:68
bool createsStress() const
Return false if subclass does not generate stress.
virtual void unsetStress()
Mark the stress as unknown.
virtual void computeStress()
Compute and store the stress tensor.
StressCalculator(bool createsStress=true)
Constructor (protected to prevent direct instantiation).