ReferenceMonteCarloBarostat.cpp 4.1 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
ReferenceMonteCarloBarostat::ReferenceMonteCarloBarostat(int numAtoms, const vector<vector<int> >& molecules) : molecules(molecules) {
41
42
43
44
45
46
47
48
49
50
51
    savedAtomPositions[0].resize(numAtoms);
    savedAtomPositions[1].resize(numAtoms);
    savedAtomPositions[2].resize(numAtoms);
}

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

  Destructor

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

52
ReferenceMonteCarloBarostat::~ReferenceMonteCarloBarostat() {
53
54
55
56
57
58
59
}

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

  Apply the barostat at the start of a time step.

  @param atomPositions      atom positions
60
  @param boxVectors         the periodic box vectors
61
62
63
  @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
64
65
66

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

peastman's avatar
peastman committed
67
void ReferenceMonteCarloBarostat::applyBarostat(vector<Vec3>& atomPositions, const Vec3* boxVectors, double scaleX, double scaleY, double scaleZ) {
68
69
70
71
72
73
74
    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.

peastman's avatar
peastman committed
75
    for (auto& molecule : molecules) {
76
77
        // Find the molecule center.

peastman's avatar
peastman committed
78
        Vec3 pos(0, 0, 0);
peastman's avatar
peastman committed
79
80
        for (int atom : molecule) {
            Vec3& atomPos = atomPositions[atom];
81
            pos += atomPos;
82
        }
peastman's avatar
peastman committed
83
        pos /= molecule.size();
84
85
86

        // Now scale the position of the molecule center.

87
        Vec3 newPos = pos;
88
89
90
        newPos[0] *= scaleX;
        newPos[1] *= scaleY;
        newPos[2] *= scaleZ;
peastman's avatar
peastman committed
91
        Vec3 offset = newPos-pos;
peastman's avatar
peastman committed
92
93
        for (int atom : molecule) {
            Vec3& atomPos = atomPositions[atom];
94
            atomPos += offset;
95
96
97
98
        }
    }
}

99
100
101
102
103
104
105
106
/**---------------------------------------------------------------------------------------

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

  @param atomPositions      atom positions

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

peastman's avatar
peastman committed
107
void ReferenceMonteCarloBarostat::restorePositions(vector<Vec3>& atomPositions) {
108
109
110
111
112
    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];
}