Context.cpp 5.03 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
40
Context::Context(System& system, Integrator& integrator) {
    impl = new ContextImpl(*this, system, integrator, 0);
41
42
}

43
44
Context::Context(System& system, Integrator& integrator, Platform& platform) {
    impl = new ContextImpl(*this, system, integrator, &platform);
45
46
}

47
Context::~Context() {
48
49
50
	delete impl;
}

51
const System& Context::getSystem() const {
52
53
54
55
    return impl->getSystem();

}

56
System& Context::getSystem() {
57
58
59
    return impl->getSystem();
}

60
const Integrator& Context::getIntegrator() const {
61
62
63
    return impl->getIntegrator();
}

64
Integrator& Context::getIntegrator() {
65
    return impl->getIntegrator();
66
67
}

68
const Platform& Context::getPlatform() const {
69
70
    return impl->getPlatform();
}
71

72
Platform& Context::getPlatform() {
73
    return impl->getPlatform();
74
75
}

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

95
void Context::setTime(double time) {
96
97
98
    impl->setTime(time);
}

99
void Context::setPositions(const vector<Vec3>& positions) {
Peter Eastman's avatar
Peter Eastman committed
100
    if ((int) positions.size() != impl->getSystem().getNumParticles())
101
        throw OpenMMException("Called setPositions() on a Context with the wrong number of positions");
102
    impl->setPositions(positions);
103
104
}

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

111
double Context::getParameter(const string& name) {
112
113
114
    return impl->getParameter(name);
}

115
void Context::setParameter(const string& name, double value) {
116
117
118
    impl->setParameter(name, value);
}

119
void Context::reinitialize() {
120
121
122
123
    System& system = impl->getSystem();
    Integrator& integrator = impl->getIntegrator();
    Platform& platform = impl->getPlatform();
    delete impl;
124
    impl = new ContextImpl(*this, system, integrator, &platform);
125
}