CustomIntegrator.cpp 9.77 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) 2011-2012 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
34
 * 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/CustomIntegrator.h"
#include "openmm/Context.h"
#include "openmm/OpenMMException.h"
35
#include "openmm/internal/AssertionUtilities.h"
36
37
38
39
40
41
42
43
44
#include "openmm/internal/ContextImpl.h"
#include "openmm/kernels.h"
#include <ctime>
#include <string>

using namespace OpenMM;
using std::string;
using std::vector;

45
CustomIntegrator::CustomIntegrator(double stepSize) : globalsAreCurrent(true), forcesAreValid(false) {
46
47
48
    setStepSize(stepSize);
    setConstraintTolerance(1e-4);
    setRandomNumberSeed((int) time(NULL));
49
    kineticEnergy = "m*v*v/2";
50
51
52
53
54
55
56
57
}

void CustomIntegrator::initialize(ContextImpl& contextRef) {
    if (owner != NULL && &contextRef.getOwner() != owner)
        throw OpenMMException("This Integrator is already bound to a context");
    context = &contextRef;
    owner = &contextRef.getOwner();
    kernel = context->getPlatform().createKernel(IntegrateCustomStepKernel::Name(), contextRef);
58
59
    kernel.getAs<IntegrateCustomStepKernel>().initialize(contextRef.getSystem(), *this);
    kernel.getAs<IntegrateCustomStepKernel>().setGlobalVariables(contextRef, globalValues);
60
61
62
    for (int i = 0; i < (int) perDofValues.size(); i++) {
        if (perDofValues[i].size() == 1)
            perDofValues[i].resize(context->getSystem().getNumParticles(), perDofValues[i][0]);
63
        kernel.getAs<IntegrateCustomStepKernel>().setPerDofVariable(contextRef, i, perDofValues[i]);
64
    }
65
66
}

67
68
69
70
void CustomIntegrator::cleanup() {
    kernel = Kernel();
}

71
72
73
74
75
void CustomIntegrator::stateChanged(State::DataType changed) {
    forcesAreValid = false;
}

vector<string> CustomIntegrator::getKernelNames() {
76
    vector<string> names;
77
78
79
80
    names.push_back(IntegrateCustomStepKernel::Name());
    return names;
}

81
82
83
84
double CustomIntegrator::computeKineticEnergy() {
    return kernel.getAs<IntegrateCustomStepKernel>().computeKineticEnergy(*context, *this, forcesAreValid);
}

85
86
87
void CustomIntegrator::step(int steps) {
    globalsAreCurrent = false;
    for (int i = 0; i < steps; ++i) {
88
        kernel.getAs<IntegrateCustomStepKernel>().execute(*context, *this, forcesAreValid);
89
90
91
    }
}

92
int CustomIntegrator::addGlobalVariable(const string& name, double initialValue) {
93
94
95
96
97
98
99
    if (owner != NULL)
        throw OpenMMException("The integrator cannot be modified after it is bound to a context");
    globalNames.push_back(name);
    globalValues.push_back(initialValue);
    return globalNames.size()-1;
}

100
const string& CustomIntegrator::getGlobalVariableName(int index) const {
101
    ASSERT_VALID_INDEX(index, globalNames);
102
103
104
    return globalNames[index];
}

105
int CustomIntegrator::addPerDofVariable(const string& name, double initialValue) {
106
107
108
109
110
111
112
    if (owner != NULL)
        throw OpenMMException("The integrator cannot be modified after it is bound to a context");
    perDofNames.push_back(name);
    perDofValues.push_back(vector<Vec3>(1, Vec3(initialValue, initialValue, initialValue)));
    return perDofNames.size()-1;
}

113
const string& CustomIntegrator::getPerDofVariableName(int index) const {
114
    ASSERT_VALID_INDEX(index, perDofNames);
115
116
117
118
    return perDofNames[index];
}

double CustomIntegrator::getGlobalVariable(int index) const {
119
    ASSERT_VALID_INDEX(index, globalValues);
120
    if (owner != NULL && !globalsAreCurrent) {
121
        kernel.getAs<const IntegrateCustomStepKernel>().getGlobalVariables(*context, globalValues);
122
123
124
125
126
127
        globalsAreCurrent = true;
    }
    return globalValues[index];
}

void CustomIntegrator::setGlobalVariable(int index, double value) {
128
    ASSERT_VALID_INDEX(index, globalValues);
129
    if (owner != NULL && !globalsAreCurrent) {
130
        kernel.getAs<IntegrateCustomStepKernel>().getGlobalVariables(*context, globalValues);
131
132
133
        globalsAreCurrent = true;
    }
    globalValues[index] = value;
134
    if (owner != NULL)
135
        kernel.getAs<IntegrateCustomStepKernel>().setGlobalVariables(*context, globalValues);
136
137
}

138
139
140
141
142
143
144
145
146
147
void CustomIntegrator::setGlobalVariableByName(const string& name, double value) {
    for (int i = 0; i < (int) globalNames.size(); i++)
        if (name == globalNames[i]) {
            setGlobalVariable(i, value);
            return;
        }
    throw OpenMMException("Illegal global variable name: "+name);
}

void CustomIntegrator::getPerDofVariable(int index, vector<Vec3>& values) const {
148
    ASSERT_VALID_INDEX(index, perDofValues);
149
150
151
    if (owner == NULL)
        values = perDofValues[index];
    else
152
        kernel.getAs<const IntegrateCustomStepKernel>().getPerDofVariable(*context, index, values);
153
154
}

155
void CustomIntegrator::setPerDofVariable(int index, const vector<Vec3>& values) {
156
    ASSERT_VALID_INDEX(index, perDofValues);
157
    if (owner != NULL && values.size() != context->getSystem().getNumParticles())
158
        throw OpenMMException("setPerDofVariable() called with wrong number of values");
159
160
161
    if (owner == NULL)
        perDofValues[index] = values;
    else
162
        kernel.getAs<IntegrateCustomStepKernel>().setPerDofVariable(*context, index, values);
163
164
}

165
166
167
168
169
170
171
172
173
174
void CustomIntegrator::setPerDofVariableByName(const string& name, const vector<Vec3>& value) {
    for (int i = 0; i < (int) perDofNames.size(); i++)
        if (name == perDofNames[i]) {
            setPerDofVariable(i, value);
            return;
        }
    throw OpenMMException("Illegal per-DOF variable name: "+name);
}

int CustomIntegrator::addComputeGlobal(const string& variable, const string& expression) {
175
176
177
178
179
180
    if (owner != NULL)
        throw OpenMMException("The integrator cannot be modified after it is bound to a context");
    computations.push_back(ComputationInfo(ComputeGlobal, variable, expression));
    return computations.size()-1;
}

181
int CustomIntegrator::addComputePerDof(const string& variable, const string& expression) {
182
183
184
185
186
187
    if (owner != NULL)
        throw OpenMMException("The integrator cannot be modified after it is bound to a context");
    computations.push_back(ComputationInfo(ComputePerDof, variable, expression));
    return computations.size()-1;
}

188
int CustomIntegrator::addComputeSum(const string& variable, const string& expression) {
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
    if (owner != NULL)
        throw OpenMMException("The integrator cannot be modified after it is bound to a context");
    computations.push_back(ComputationInfo(ComputeSum, variable, expression));
    return computations.size()-1;
}

int CustomIntegrator::addConstrainPositions() {
    if (owner != NULL)
        throw OpenMMException("The integrator cannot be modified after it is bound to a context");
    computations.push_back(ComputationInfo(ConstrainPositions, "", ""));
    return computations.size()-1;
}

int CustomIntegrator::addConstrainVelocities() {
    if (owner != NULL)
        throw OpenMMException("The integrator cannot be modified after it is bound to a context");
    computations.push_back(ComputationInfo(ConstrainVelocities, "", ""));
    return computations.size()-1;
}

int CustomIntegrator::addUpdateContextState() {
    if (owner != NULL)
        throw OpenMMException("The integrator cannot be modified after it is bound to a context");
    computations.push_back(ComputationInfo(UpdateContextState, "", ""));
    return computations.size()-1;
}

216
void CustomIntegrator::getComputationStep(int index, ComputationType& type, string& variable, string& expression) const {
217
    ASSERT_VALID_INDEX(index, computations);
218
219
220
221
    type = computations[index].type;
    variable = computations[index].variable;
    expression = computations[index].expression;
}
222
223
224
225
226
227
228
229

const string& CustomIntegrator::getKineticEnergyExpression() const {
    return kineticEnergy;
}

void CustomIntegrator::setKineticEnergyExpression(const string& expression) {
    kineticEnergy = expression;
}