OpenMMContext.cpp 5.14 KB
Newer Older
1
2
3
4
5
6
7
8
9
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
35
36
37
38
/* -------------------------------------------------------------------------- *
 *                                   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.               *
 *                                                                            *
 * Portions copyright (c) 2008 Stanford University and the Authors.           *
 * 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 "OpenMMContext.h"
#include "internal/OpenMMContextImpl.h"
#include "OpenMMException.h"

using namespace OpenMM;
using namespace std;

39
40
41
42
OpenMMContext::OpenMMContext(System& system, Integrator& integrator) : impl(new OpenMMContextImpl(*this, system, integrator, 0)) {
}

OpenMMContext::OpenMMContext(System& system, Integrator& integrator, Platform& platform) : impl(new OpenMMContextImpl(*this, system, integrator, &platform)) {
43
44
}

45
46
47
48
OpenMMContext::~OpenMMContext() {
	delete impl;
}

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const System& OpenMMContext::getSystem() const {
    return impl->getSystem();

}

System& OpenMMContext::getSystem() {
    return impl->getSystem();
}

const Integrator& OpenMMContext::getIntegrator() const {
    return impl->getIntegrator();

}

Integrator& OpenMMContext::getIntegrator() {
    return impl->getIntegrator();

}

68
State OpenMMContext::getState(int types) const {
Peter Eastman's avatar
Peter Eastman committed
69
    State state(impl->getTime(), impl->getSystem().getNumParticles(), State::DataType(types));
70
71
    if (types&State::Energy)
        state.setEnergy(impl->calcKineticEnergy(), impl->calcPotentialEnergy());
72
73
    if (types&State::Forces) {
        impl->calcForces();
74
        impl->getForces().saveToArray(&state.updForces()[0]);
75
    }
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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)
        impl->getPositions().saveToArray(&state.updPositions()[0]);
    if (types&State::Velocities)
        impl->getVelocities().saveToArray(&state.updVelocities()[0]);
    return state;
}

double OpenMMContext::getTime() {
    return impl->getTime();
}

void OpenMMContext::setTime(double time) {
    impl->setTime(time);
}

void OpenMMContext::setPositions(const vector<Vec3>& positions) {
Peter Eastman's avatar
Peter Eastman committed
96
    if ((int) positions.size() != impl->getSystem().getNumParticles())
97
98
99
100
101
        throw OpenMMException("Called setPositions() on an OpenMMContext with the wrong number of positions");
    impl->getPositions().loadFromArray(&positions[0]);
}

void OpenMMContext::setVelocities(const vector<Vec3>& velocities) {
Peter Eastman's avatar
Peter Eastman committed
102
    if ((int) velocities.size() != impl->getSystem().getNumParticles())
103
104
105
106
107
108
109
110
111
112
113
114
115
        throw OpenMMException("Called setVelocities() on an OpenMMContext with the wrong number of velocities");
    impl->getVelocities().loadFromArray(&velocities[0]);
}

double OpenMMContext::getParameter(string name) {
    return impl->getParameter(name);
}

void OpenMMContext::setParameter(string name, double value) {
    impl->setParameter(name, value);
}

void OpenMMContext::reinitialize() {
116
117
118
119
120
    System& system = impl->getSystem();
    Integrator& integrator = impl->getIntegrator();
    Platform& platform = impl->getPlatform();
    delete impl;
    impl = new OpenMMContextImpl(*this, system, integrator, &platform);
121
}