MonteCarloBarostatImpl.cpp 6.08 KB
Newer Older
1
2
3
4
5
6
7
8
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
 * This is part of the OpenMM molecular simulation toolkit originating from   *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
9
 * Portions copyright (c) 2010-2020 Stanford University and the Authors.      *
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
 * 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 "openmm/internal/MonteCarloBarostatImpl.h"
#include "openmm/internal/ContextImpl.h"
34
#include "openmm/internal/OSRngSeed.h"
35
36
#include "openmm/Context.h"
#include "openmm/kernels.h"
37
#include "openmm/OpenMMException.h"
38
#include "SimTKOpenMMUtilities.h"
39
40
#include <cmath>
#include <vector>
41
#include <algorithm>
42
43

using namespace OpenMM;
44
using namespace std;
45

46
MonteCarloBarostatImpl::MonteCarloBarostatImpl(const MonteCarloBarostat& owner) : owner(owner), step(0) {
47
48
49
}

void MonteCarloBarostatImpl::initialize(ContextImpl& context) {
50
51
    if (!context.getSystem().usesPeriodicBoundaryConditions())
        throw OpenMMException("A barostat cannot be used with a non-periodic system");
52
    kernel = context.getPlatform().createKernel(ApplyMonteCarloBarostatKernel::Name(), context);
53
    kernel.getAs<ApplyMonteCarloBarostatKernel>().initialize(context.getSystem(), owner);
54
    Vec3 box[3];
55
    context.getPeriodicBoxVectors(box[0], box[1], box[2]);
56
57
    double volume = box[0][0]*box[1][1]*box[2][2];
    volumeScale = 0.01*volume;
58
59
    numAttempted = 0;
    numAccepted = 0;
60
    SimTKOpenMMUtilities::setRandomNumberSeed(owner.getRandomNumberSeed());
61
62
}

63
void MonteCarloBarostatImpl::updateContextState(ContextImpl& context, bool& forcesInvalid) {
64
    if (++step < owner.getFrequency() || owner.getFrequency() == 0)
65
66
        return;
    step = 0;
67

68
    // Compute the current potential energy.
69

70
71
    int groups = context.getIntegrator().getIntegrationForceGroups();
    double initialEnergy = context.getOwner().getState(State::Energy, false, groups).getPotentialEnergy();
72

73
    // Modify the periodic box size.
74

75
    Vec3 box[3];
76
    context.getPeriodicBoxVectors(box[0], box[1], box[2]);
77
    double volume = box[0][0]*box[1][1]*box[2][2];
78
    double deltaVolume = volumeScale*2*(SimTKOpenMMUtilities::getUniformlyDistributedRandomNumber()-0.5);
79
    double newVolume = volume+deltaVolume;
80
    double lengthScale = pow(newVolume/volume, 1.0/3.0);
Lee-Ping's avatar
Lee-Ping committed
81
    kernel.getAs<ApplyMonteCarloBarostatKernel>().scaleCoordinates(context, lengthScale, lengthScale, lengthScale);
82
    context.getOwner().setPeriodicBoxVectors(box[0]*lengthScale, box[1]*lengthScale, box[2]*lengthScale);
83

84
85
    // Compute the energy of the modified system.
    
86
    double finalEnergy = context.getOwner().getState(State::Energy, false, groups).getPotentialEnergy();
87
    double pressure = context.getParameter(MonteCarloBarostat::Pressure())*(AVOGADRO*1e-25);
88
    double kT = BOLTZ*context.getParameter(MonteCarloBarostat::Temperature());
89
90
    double w = finalEnergy-initialEnergy + pressure*deltaVolume - context.getMolecules().size()*kT*log(newVolume/volume);
    if (w > 0 && SimTKOpenMMUtilities::getUniformlyDistributedRandomNumber() > exp(-w/kT)) {
91
        // Reject the step.
92

93
        context.getOwner().setPeriodicBoxVectors(box[0], box[1], box[2]);
94
        kernel.getAs<ApplyMonteCarloBarostatKernel>().restoreCoordinates(context);
95
    }
96
    else {
97
        numAccepted++;
98
99
        forcesInvalid = true;
    }
100
101
102
103
104
105
106
107
    numAttempted++;
    if (numAttempted >= 10) {
        if (numAccepted < 0.25*numAttempted) {
            volumeScale /= 1.1;
            numAttempted = 0;
            numAccepted = 0;
        }
        else if (numAccepted > 0.75*numAttempted) {
108
            volumeScale = min(volumeScale*1.1, volume*0.3);
109
110
111
112
            numAttempted = 0;
            numAccepted = 0;
        }
    }
113
114
}

115
116
map<string, double> MonteCarloBarostatImpl::getDefaultParameters() {
    map<string, double> parameters;
117
    parameters[MonteCarloBarostat::Pressure()] = getOwner().getDefaultPressure();
118
    parameters[MonteCarloBarostat::Temperature()] = getOwner().getDefaultTemperature();
119
120
121
    return parameters;
}

122
123
vector<string> MonteCarloBarostatImpl::getKernelNames() {
    vector<string> names;
124
125
126
127
    names.push_back(ApplyMonteCarloBarostatKernel::Name());
    return names;
}