ReferenceMonteCarloBarostat.cpp 5.47 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

/* Portions copyright (c) 2010 Stanford University and Simbios.
 * Contributors: Peter Eastman
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject
 * to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <string.h>
#include <sstream>

28
#include "SimTKOpenMMUtilities.h"
29
30
31
#include "ReferenceMonteCarloBarostat.h"

using namespace std;
32
using namespace OpenMM;
33
34
35
36
37
38
39

/**---------------------------------------------------------------------------------------

  Constructor

  --------------------------------------------------------------------------------------- */

40
41
ReferenceMonteCarloBarostat::ReferenceMonteCarloBarostat(int numAtoms, const vector<vector<int> >& molecules, const std::vector<double>& masses) :
            molecules(molecules), masses(masses) {
42
43
44
45
46
47
48
49
50
51
52
    savedAtomPositions[0].resize(numAtoms);
    savedAtomPositions[1].resize(numAtoms);
    savedAtomPositions[2].resize(numAtoms);
}

/**---------------------------------------------------------------------------------------

  Destructor

  --------------------------------------------------------------------------------------- */

53
ReferenceMonteCarloBarostat::~ReferenceMonteCarloBarostat() {
54
55
}

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**---------------------------------------------------------------------------------------

  Save the positions before applying the barostat.

  @param atomPositions      atom positions

  --------------------------------------------------------------------------------------- */

void ReferenceMonteCarloBarostat::savePositions(vector<Vec3>& atomPositions) {
    int numAtoms = savedAtomPositions[0].size();
    for (int i = 0; i < numAtoms; i++)
        for (int j = 0; j < 3; j++)
            savedAtomPositions[j][i] = atomPositions[i][j];
}

71
72
73
74
75
/**---------------------------------------------------------------------------------------

  Apply the barostat at the start of a time step.

  @param atomPositions      atom positions
76
  @param boxVectors         the periodic box vectors
77
78
79
  @param scaleX             the factor by which to scale atom x-coordinates
  @param scaleY             the factor by which to scale atom y-coordinates
  @param scaleZ             the factor by which to scale atom z-coordinates
80
81
82

  --------------------------------------------------------------------------------------- */

peastman's avatar
peastman committed
83
void ReferenceMonteCarloBarostat::applyBarostat(vector<Vec3>& atomPositions, const Vec3* boxVectors, double scaleX, double scaleY, double scaleZ) {
84
85
    // Loop over molecules.

peastman's avatar
peastman committed
86
    for (auto& molecule : molecules) {
87
88
        // Find the molecule center.

peastman's avatar
peastman committed
89
        Vec3 pos(0, 0, 0);
peastman's avatar
peastman committed
90
91
        for (int atom : molecule) {
            Vec3& atomPos = atomPositions[atom];
92
            pos += atomPos;
93
        }
peastman's avatar
peastman committed
94
        pos /= molecule.size();
95
96
97

        // Now scale the position of the molecule center.

98
        Vec3 newPos = pos;
99
100
101
        newPos[0] *= scaleX;
        newPos[1] *= scaleY;
        newPos[2] *= scaleZ;
peastman's avatar
peastman committed
102
        Vec3 offset = newPos-pos;
peastman's avatar
peastman committed
103
104
        for (int atom : molecule) {
            Vec3& atomPos = atomPositions[atom];
105
            atomPos += offset;
106
107
108
109
        }
    }
}

110
111
/**---------------------------------------------------------------------------------------

112
  Restore atom positions to what they were before savePositions() was called.
113
114
115
116
117

  @param atomPositions      atom positions

  --------------------------------------------------------------------------------------- */

peastman's avatar
peastman committed
118
void ReferenceMonteCarloBarostat::restorePositions(vector<Vec3>& atomPositions) {
119
120
121
122
123
    int numAtoms = savedAtomPositions[0].size();
    for (int i = 0; i < numAtoms; i++)
        for (int j = 0; j < 3; j++)
            atomPositions[i][j] = savedAtomPositions[j][i];
}
124
125
126
127
128
129
130
131
132
133
134
135

void ReferenceMonteCarloBarostat::computeMolecularKineticEnergy(const vector<Vec3>& velocities, vector<double>& ke, int components) {
    ke.resize(components);
    for (int i = 0; i < components; i++)
        ke[i] = 0.0;
    for (auto& molecule : molecules) {
        Vec3 molVel;
        double molMass = 0.0;
        for (int atom : molecule) {
            molVel += masses[atom]*velocities[atom];
            molMass += masses[atom];
        }
136
        molVel /= molMass;
137
138
139
140
141
142
143
144
145
146
147
148
149
150
        if (components == 1)
            ke[0] += 0.5*molMass*molVel.dot(molVel);
        else {
            ke[0] += 0.5*molMass*molVel[0]*molVel[0];
            ke[1] += 0.5*molMass*molVel[1]*molVel[1];
            ke[2] += 0.5*molMass*molVel[2]*molVel[2];
            if (components == 6) {
                ke[3] += 0.5*molMass*molVel[1]*molVel[0];
                ke[4] += 0.5*molMass*molVel[2]*molVel[0];
                ke[5] += 0.5*molMass*molVel[2]*molVel[1];
            }
        }
    }
}