ReferenceMonteCarloBarostat.cpp 6.23 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
28
29
30
31

/* 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>

#include "../SimTKUtilities/SimTKOpenMMUtilities.h"
#include "ReferenceMonteCarloBarostat.h"

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

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

  Constructor

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

40
ReferenceMonteCarloBarostat::ReferenceMonteCarloBarostat(int numAtoms, const vector<vector<int> >& molecules) : molecules(molecules) {
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    savedAtomPositions[0].resize(numAtoms);
    savedAtomPositions[1].resize(numAtoms);
    savedAtomPositions[2].resize(numAtoms);
}

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

  Destructor

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

ReferenceMonteCarloBarostat::~ReferenceMonteCarloBarostat( ) {
}

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

  Apply the barostat at the start of a time step.

  @param atomPositions      atom positions
  @param boxSize            the periodic box dimensions
  @param scale              the factor by which to scale atom positions

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

65
void ReferenceMonteCarloBarostat::applyBarostat(vector<RealVec>& atomPositions, const RealVec& boxSize, RealOpenMM scale) {
66
67
68
69
70
    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
    // Loop over molecules.
72

73
74
    for (int i = 0; i < (int) molecules.size(); i++) {
        // Find the molecule center.
75
76

        RealOpenMM pos[3] = {0, 0, 0};
77
        for (int j = 0; j < (int) molecules[i].size(); j++) {
78
            RealVec& atomPos = atomPositions[molecules[i][j]];
79
80
81
82
            pos[0] += atomPos[0];
            pos[1] += atomPos[1];
            pos[2] += atomPos[2];
        }
83
84
85
        pos[0] /= molecules[i].size();
        pos[1] /= molecules[i].size();
        pos[2] /= molecules[i].size();
86
87
88
89
90
91

        // Move it into the first periodic box.

        int xcell = (int) floor(pos[0]/boxSize[0]);
        int ycell = (int) floor(pos[1]/boxSize[1]);
        int zcell = (int) floor(pos[2]/boxSize[2]);
92
93
94
        RealOpenMM dx = xcell*boxSize[0];
        RealOpenMM dy = ycell*boxSize[1];
        RealOpenMM dz = zcell*boxSize[2];
95
96
97
98
        pos[0] -= dx;
        pos[1] -= dy;
        pos[2] -= dz;

99
        // Now scale the position of the molecule center.
100

101
102
103
104
        dx = pos[0]*(scale-1)-dx;
        dy = pos[1]*(scale-1)-dy;
        dz = pos[2]*(scale-1)-dz;
        for (int j = 0; j < (int) molecules[i].size(); j++) {
105
            RealVec& atomPos = atomPositions[molecules[i][j]];
106
107
108
109
110
111
112
            atomPos[0] += dx;
            atomPos[1] += dy;
            atomPos[2] += dz;
        }
    }
}

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
void ReferenceMonteCarloBarostat::applyBarostatXYZ(vector<RealVec>& atomPositions, const RealVec& boxSize, RealOpenMM scaleX, RealOpenMM scaleY, RealOpenMM scaleZ) {
    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];

    // Loop over molecules.

    for (int i = 0; i < (int) molecules.size(); i++) {
        // Find the molecule center.

        RealOpenMM pos[3] = {0, 0, 0};
        for (int j = 0; j < (int) molecules[i].size(); j++) {
            RealVec& atomPos = atomPositions[molecules[i][j]];
            pos[0] += atomPos[0];
            pos[1] += atomPos[1];
            pos[2] += atomPos[2];
        }
        pos[0] /= molecules[i].size();
        pos[1] /= molecules[i].size();
        pos[2] /= molecules[i].size();

        // Move it into the first periodic box.

        int xcell = (int) floor(pos[0]/boxSize[0]);
        int ycell = (int) floor(pos[1]/boxSize[1]);
        int zcell = (int) floor(pos[2]/boxSize[2]);
        RealOpenMM dx = xcell*boxSize[0];
        RealOpenMM dy = ycell*boxSize[1];
        RealOpenMM dz = zcell*boxSize[2];
        pos[0] -= dx;
        pos[1] -= dy;
        pos[2] -= dz;

        // Now scale the position of the molecule center.

        dx = pos[0]*(scaleX-1)-dx;
        dy = pos[1]*(scaleY-1)-dy;
        dz = pos[2]*(scaleZ-1)-dz;
        for (int j = 0; j < (int) molecules[i].size(); j++) {
            RealVec& atomPos = atomPositions[molecules[i][j]];
            atomPos[0] += dx;
            atomPos[1] += dy;
            atomPos[2] += dz;
        }
    }
}

161
162
163
164
165
166
167
168
/**---------------------------------------------------------------------------------------

  Restore atom positions to what they were before applyBarostat() was called.

  @param atomPositions      atom positions

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

169
void ReferenceMonteCarloBarostat::restorePositions(vector<RealVec>& atomPositions) {
170
171
172
173
174
    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];
}