Context.cpp 7.62 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.               *
 *                                                                            *
Peter Eastman's avatar
Peter Eastman committed
9
 * Portions copyright (c) 2008-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
 * 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.                                     *
 * -------------------------------------------------------------------------- */

32
33
#include "openmm/Context.h"
#include "openmm/internal/ContextImpl.h"
34
#include "openmm/OpenMMException.h"
35
#include "openmm/internal/ForceImpl.h"
36
#include <cmath>
37
38
39
40

using namespace OpenMM;
using namespace std;

41
Context::Context(System& system, Integrator& integrator) : properties(map<string, string>()) {
42
    impl = new ContextImpl(*this, system, integrator, 0, properties);
43
44
}

45
Context::Context(System& system, Integrator& integrator, Platform& platform) : properties(map<string, string>()) {
46
    impl = new ContextImpl(*this, system, integrator, &platform, properties);
47
48
49
}

Context::Context(System& system, Integrator& integrator, Platform& platform, const map<string, string>& properties) : properties(properties) {
50
    impl = new ContextImpl(*this, system, integrator, &platform, properties);
51
52
}

53
Context::~Context() {
54
55
56
	delete impl;
}

57
const System& Context::getSystem() const {
58
59
60
61
    return impl->getSystem();

}

62
System& Context::getSystem() {
63
64
65
    return impl->getSystem();
}

66
const Integrator& Context::getIntegrator() const {
67
68
69
    return impl->getIntegrator();
}

70
Integrator& Context::getIntegrator() {
71
    return impl->getIntegrator();
72
73
}

74
const Platform& Context::getPlatform() const {
75
76
    return impl->getPlatform();
}
77

78
Platform& Context::getPlatform() {
79
    return impl->getPlatform();
80
81
}

82
State Context::getState(int types, bool enforcePeriodicBox, int groups) const {
83
    State state(impl->getTime(), impl->getSystem().getNumParticles(), types);
84
85
86
    Vec3 periodicBoxSize[3];
    impl->getPeriodicBoxVectors(periodicBoxSize[0], periodicBoxSize[1], periodicBoxSize[2]);
    state.setPeriodicBoxVectors(periodicBoxSize[0], periodicBoxSize[1], periodicBoxSize[2]);
87
88
89
    bool includeForces = types&State::Forces;
    bool includeEnergy = types&State::Energy;
    if (includeForces || includeEnergy) {
90
        double energy = impl->calcForcesAndEnergy(includeForces || includeEnergy, includeEnergy, groups);
91
92
93
94
        if (includeEnergy)
            state.setEnergy(impl->calcKineticEnergy(), energy);
        if (includeForces)
            impl->getForces(state.updForces());
95
    }
96
97
98
99
    if (types&State::Parameters) {
        for (map<string, double>::const_iterator iter = impl->parameters.begin(); iter != impl->parameters.end(); iter++)
            state.updParameters()[iter->first] = iter->second;
    }
100
    if (types&State::Positions) {
101
        impl->getPositions(state.updPositions());
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
        if (enforcePeriodicBox) {
            const vector<vector<int> >& molecules = impl->getMolecules();
            vector<Vec3>& positions = state.updPositions();
            for (int i = 0; i < (int) molecules.size(); i++) {
                // Find the molecule center.

                Vec3 center;
                for (int j = 0; j < (int) molecules[i].size(); j++)
                    center += positions[molecules[i][j]];
                center *= 1.0/molecules[i].size();

                // Find the displacement to move it into the first periodic box.

                int xcell = (int) floor(center[0]/periodicBoxSize[0][0]);
                int ycell = (int) floor(center[1]/periodicBoxSize[1][1]);
                int zcell = (int) floor(center[2]/periodicBoxSize[2][2]);
                double dx = xcell*periodicBoxSize[0][0];
                double dy = ycell*periodicBoxSize[1][1];
                double dz = zcell*periodicBoxSize[2][2];

                // Translate all the particles in the molecule.
                
                for (int j = 0; j < (int) molecules[i].size(); j++) {
                    Vec3& pos = positions[molecules[i][j]];
                    pos[0] -= dx;
                    pos[1] -= dy;
                    pos[2] -= dz;
                }
            }
        }
    }
133
    if (types&State::Velocities)
134
        impl->getVelocities(state.updVelocities());
135
136
137
    return state;
}

138
void Context::setTime(double time) {
139
140
141
    impl->setTime(time);
}

142
void Context::setPositions(const vector<Vec3>& positions) {
Peter Eastman's avatar
Peter Eastman committed
143
    if ((int) positions.size() != impl->getSystem().getNumParticles())
144
        throw OpenMMException("Called setPositions() on a Context with the wrong number of positions");
145
    impl->setPositions(positions);
146
147
}

148
void Context::setVelocities(const vector<Vec3>& velocities) {
Peter Eastman's avatar
Peter Eastman committed
149
    if ((int) velocities.size() != impl->getSystem().getNumParticles())
150
        throw OpenMMException("Called setVelocities() on a Context with the wrong number of velocities");
151
    impl->setVelocities(velocities);
152
153
}

154
double Context::getParameter(const string& name) {
155
156
157
    return impl->getParameter(name);
}

158
void Context::setParameter(const string& name, double value) {
159
160
161
    impl->setParameter(name, value);
}

162
163
void Context::setPeriodicBoxVectors(const Vec3& a, const Vec3& b, const Vec3& c) {
    impl->setPeriodicBoxVectors(a, b, c);
164
165
}

166
167
168
169
void Context::applyConstraints(double tol) {
    impl->applyConstraints(tol);
}

170
171
172
173
void Context::computeVirtualSites() {
    impl->computeVirtualSites();
}

174
void Context::reinitialize() {
175
176
177
178
    System& system = impl->getSystem();
    Integrator& integrator = impl->getIntegrator();
    Platform& platform = impl->getPlatform();
    delete impl;
179
    impl = new ContextImpl(*this, system, integrator, &platform, properties);
180
}
Peter Eastman's avatar
Peter Eastman committed
181
182
183
184
185
186
187
188

void Context::createCheckpoint(ostream& stream) {
    impl->createCheckpoint(stream);
}

void Context::loadCheckpoint(istream& stream) {
    impl->loadCheckpoint(stream);
}
189
190
191
192

ContextImpl& Context::getImpl() {
    return *impl;
}