Context.cpp 6.46 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) 2008-2009 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
36
37
38

using namespace OpenMM;
using namespace std;

39
Context::Context(System& system, Integrator& integrator) : properties(map<string, string>()) {
40
    system.getDefaultPeriodicBoxVectors(periodicBoxVectors[0], periodicBoxVectors[1], periodicBoxVectors[2]);
41
    impl = new ContextImpl(*this, system, integrator, 0, properties);
42
43
}

44
Context::Context(System& system, Integrator& integrator, Platform& platform) : properties(map<string, string>()) {
45
    system.getDefaultPeriodicBoxVectors(periodicBoxVectors[0], periodicBoxVectors[1], periodicBoxVectors[2]);
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
    system.getDefaultPeriodicBoxVectors(periodicBoxVectors[0], periodicBoxVectors[1], periodicBoxVectors[2]);
51
    impl = new ContextImpl(*this, system, integrator, &platform, properties);
52
53
}

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

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

}

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

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

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

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

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

83
State Context::getState(int types) const {
Peter Eastman's avatar
Peter Eastman committed
84
    State state(impl->getTime(), impl->getSystem().getNumParticles(), State::DataType(types));
85
86
    if (types&State::Energy)
        state.setEnergy(impl->calcKineticEnergy(), impl->calcPotentialEnergy());
87
88
    if (types&State::Forces) {
        impl->calcForces();
89
        impl->getForces(state.updForces());
90
    }
91
92
93
94
95
    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;
    }
    if (types&State::Positions)
96
        impl->getPositions(state.updPositions());
97
    if (types&State::Velocities)
98
        impl->getVelocities(state.updVelocities());
99
100
101
    return state;
}

102
void Context::setTime(double time) {
103
104
105
    impl->setTime(time);
}

106
void Context::setPositions(const vector<Vec3>& positions) {
Peter Eastman's avatar
Peter Eastman committed
107
    if ((int) positions.size() != impl->getSystem().getNumParticles())
108
        throw OpenMMException("Called setPositions() on a Context with the wrong number of positions");
109
    impl->setPositions(positions);
110
111
}

112
void Context::setVelocities(const vector<Vec3>& velocities) {
Peter Eastman's avatar
Peter Eastman committed
113
    if ((int) velocities.size() != impl->getSystem().getNumParticles())
114
        throw OpenMMException("Called setVelocities() on a Context with the wrong number of velocities");
115
    impl->setVelocities(velocities);
116
117
}

118
double Context::getParameter(const string& name) {
119
120
121
    return impl->getParameter(name);
}

122
void Context::setParameter(const string& name, double value) {
123
124
125
    impl->setParameter(name, value);
}

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
void Context::getPeriodicBoxVectors(Vec3& a, Vec3& b, Vec3& c) const {
    a = periodicBoxVectors[0];
    b = periodicBoxVectors[1];
    c = periodicBoxVectors[2];
}

void Context::setPeriodicBoxVectors(Vec3 a, Vec3 b, Vec3 c) {
    if (a[1] != 0.0 || a[2] != 0.0)
        throw OpenMMException("First periodic box vector must be parallel to x.");
    if (b[0] != 0.0 || b[2] != 0.0)
        throw OpenMMException("Second periodic box vector must be parallel to y.");
    if (c[0] != 0.0 || c[1] != 0.0)
        throw OpenMMException("Third periodic box vector must be parallel to z.");
    periodicBoxVectors[0] = a;
    periodicBoxVectors[1] = b;
    periodicBoxVectors[2] = c;
}

144
void Context::reinitialize() {
145
146
147
    System& system = impl->getSystem();
    Integrator& integrator = impl->getIntegrator();
    Platform& platform = impl->getPlatform();
148
    system.getDefaultPeriodicBoxVectors(periodicBoxVectors[0], periodicBoxVectors[1], periodicBoxVectors[2]);
149
    delete impl;
150
    impl = new ContextImpl(*this, system, integrator, &platform, properties);
151
}