CustomIntegrator.cpp 11.1 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-2014 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
#include "openmm/internal/ContextImpl.h"
#include "openmm/kernels.h"
38
#include <set>
39
40
41
#include <string>

using namespace OpenMM;
42
using namespace std;
43

44
CustomIntegrator::CustomIntegrator(double stepSize) : globalsAreCurrent(true), forcesAreValid(false) {
45
    setStepSize(stepSize);
46
    setConstraintTolerance(1e-5);
47
    setRandomNumberSeed(0);
48
    kineticEnergy = "m*v*v/2";
49
50
51
52
53
}

void CustomIntegrator::initialize(ContextImpl& contextRef) {
    if (owner != NULL && &contextRef.getOwner() != owner)
        throw OpenMMException("This Integrator is already bound to a context");
54
55
56
57
58
59
60
61
62
63
64
65
    vector<std::string> variableList;
    set<std::string> variableSet;
    variableList.insert(variableList.end(), globalNames.begin(), globalNames.end());
    variableList.insert(variableList.end(), perDofNames.begin(), perDofNames.end());
    for (int i = 0; i < (int) variableList.size(); i++) {
        string& name = variableList[i];
        if (variableSet.find(name) != variableSet.end())
            throw OpenMMException("The Integrator defines two variables with the same name: "+name);
        variableSet.insert(name);
        if (contextRef.getParameters().find(name) != contextRef.getParameters().end())
            throw OpenMMException("The Integrator defines a variable with the same name as a Context parameter: "+name);
    }
66
67
68
    context = &contextRef;
    owner = &contextRef.getOwner();
    kernel = context->getPlatform().createKernel(IntegrateCustomStepKernel::Name(), contextRef);
69
70
    kernel.getAs<IntegrateCustomStepKernel>().initialize(contextRef.getSystem(), *this);
    kernel.getAs<IntegrateCustomStepKernel>().setGlobalVariables(contextRef, globalValues);
71
72
73
    for (int i = 0; i < (int) perDofValues.size(); i++) {
        if (perDofValues[i].size() == 1)
            perDofValues[i].resize(context->getSystem().getNumParticles(), perDofValues[i][0]);
74
        kernel.getAs<IntegrateCustomStepKernel>().setPerDofVariable(contextRef, i, perDofValues[i]);
75
    }
76
77
}

78
79
80
81
void CustomIntegrator::cleanup() {
    kernel = Kernel();
}

82
83
84
85
86
void CustomIntegrator::stateChanged(State::DataType changed) {
    forcesAreValid = false;
}

vector<string> CustomIntegrator::getKernelNames() {
87
    vector<string> names;
88
89
90
91
    names.push_back(IntegrateCustomStepKernel::Name());
    return names;
}

92
93
94
95
double CustomIntegrator::computeKineticEnergy() {
    return kernel.getAs<IntegrateCustomStepKernel>().computeKineticEnergy(*context, *this, forcesAreValid);
}

96
97
98
void CustomIntegrator::step(int steps) {
    globalsAreCurrent = false;
    for (int i = 0; i < steps; ++i) {
99
        kernel.getAs<IntegrateCustomStepKernel>().execute(*context, *this, forcesAreValid);
100
101
102
    }
}

103
int CustomIntegrator::addGlobalVariable(const string& name, double initialValue) {
104
105
106
107
108
109
110
    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;
}

111
const string& CustomIntegrator::getGlobalVariableName(int index) const {
112
    ASSERT_VALID_INDEX(index, globalNames);
113
114
115
    return globalNames[index];
}

116
int CustomIntegrator::addPerDofVariable(const string& name, double initialValue) {
117
118
119
120
121
122
123
    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;
}

124
const string& CustomIntegrator::getPerDofVariableName(int index) const {
125
    ASSERT_VALID_INDEX(index, perDofNames);
126
127
128
129
    return perDofNames[index];
}

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

138
double CustomIntegrator::getGlobalVariableByName(const string& name) const {
Robert McGibbon's avatar
Robert McGibbon committed
139
    for (int i = 0; i < (int) globalNames.size(); i++) {
140
141
142
143
144
145
146
        if (name == globalNames[i]) {
            return getGlobalVariable(i);
        }
    }
    throw OpenMMException("Illegal global variable name: "+name);
}

147
void CustomIntegrator::setGlobalVariable(int index, double value) {
148
    ASSERT_VALID_INDEX(index, globalValues);
149
    if (owner != NULL && !globalsAreCurrent) {
150
        kernel.getAs<IntegrateCustomStepKernel>().getGlobalVariables(*context, globalValues);
151
152
153
        globalsAreCurrent = true;
    }
    globalValues[index] = value;
154
    if (owner != NULL)
155
        kernel.getAs<IntegrateCustomStepKernel>().setGlobalVariables(*context, globalValues);
156
157
}

158
void CustomIntegrator::setGlobalVariableByName(const string& name, double value) {
159
    for (int i = 0; i < (int) globalNames.size(); i++)
160
161
162
163
164
165
166
167
        if (name == globalNames[i]) {
            setGlobalVariable(i, value);
            return;
        }
    throw OpenMMException("Illegal global variable name: "+name);
}

void CustomIntegrator::getPerDofVariable(int index, vector<Vec3>& values) const {
168
    ASSERT_VALID_INDEX(index, perDofValues);
169
170
171
    if (owner == NULL)
        values = perDofValues[index];
    else
172
        kernel.getAs<const IntegrateCustomStepKernel>().getPerDofVariable(*context, index, values);
173
174
}

175
void CustomIntegrator::getPerDofVariableByName(const string& name,  vector<Vec3>& values) const {
Robert McGibbon's avatar
Robert McGibbon committed
176
    for (int i = 0; i < (int) perDofNames.size(); i++) {
177
178
179
180
181
182
183
184
        if (name == perDofNames[i]) {
            getPerDofVariable(i, values);
            return;
        }
    }
    throw OpenMMException("Illegal per-DOF variable name: "+name);
}

185
void CustomIntegrator::setPerDofVariable(int index, const vector<Vec3>& values) {
186
    ASSERT_VALID_INDEX(index, perDofValues);
187
    if (owner != NULL && values.size() != context->getSystem().getNumParticles())
188
        throw OpenMMException("setPerDofVariable() called with wrong number of values");
189
190
191
    if (owner == NULL)
        perDofValues[index] = values;
    else
192
        kernel.getAs<IntegrateCustomStepKernel>().setPerDofVariable(*context, index, values);
193
194
}

195
196
197
198
199
200
201
202
203
204
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) {
205
206
207
208
209
210
    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;
}

211
int CustomIntegrator::addComputePerDof(const string& variable, const string& expression) {
212
213
214
215
216
217
    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;
}

218
int CustomIntegrator::addComputeSum(const string& variable, const string& expression) {
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
    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;
}

246
void CustomIntegrator::getComputationStep(int index, ComputationType& type, string& variable, string& expression) const {
247
    ASSERT_VALID_INDEX(index, computations);
248
249
250
251
    type = computations[index].type;
    variable = computations[index].variable;
    expression = computations[index].expression;
}
252
253
254
255
256
257
258
259

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

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