ContextImpl.cpp 6.82 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
/* -------------------------------------------------------------------------- *
 *                                   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.                                     *
 * -------------------------------------------------------------------------- */

32
33
34
35
36
37
#include "openmm/Force.h"
#include "openmm/Integrator.h"
#include "openmm/OpenMMException.h"
#include "openmm/System.h"
#include "openmm/kernels.h"
#include "openmm/internal/ForceImpl.h"
38
#include "openmm/internal/ContextImpl.h"
39
#include <map>
40
41
42
#include <vector>

using namespace OpenMM;
43
using std::map;
44
45
46
using std::vector;
using std::string;

47
ContextImpl::ContextImpl(Context& owner, System& system, Integrator& integrator, Platform* platform) :
48
49
				owner(owner), system(system), 
				integrator(integrator), platform(platform), 
50
				platformData(NULL)
51
{
52
53
    vector<string> kernelNames;
    kernelNames.push_back(CalcKineticEnergyKernel::Name());
54
    kernelNames.push_back(InitializeForcesKernel::Name());
55
    kernelNames.push_back(UpdateTimeKernel::Name());
56
    for (int i = 0; i < system.getNumForces(); ++i) {
57
        forceImpls.push_back(system.getForce(i).createImpl());
58
59
        map<string, double> forceParameters = forceImpls[forceImpls.size()-1]->getDefaultParameters();
        parameters.insert(forceParameters.begin(), forceParameters.end());
60
61
62
63
64
        vector<string> forceKernels = forceImpls[forceImpls.size()-1]->getKernelNames();
        kernelNames.insert(kernelNames.begin(), forceKernels.begin(), forceKernels.end());
    }
    vector<string> integratorKernels = integrator.getKernelNames();
    kernelNames.insert(kernelNames.begin(), integratorKernels.begin(), integratorKernels.end());
65
    if (platform == 0)
66
        this->platform = platform = &Platform::findPlatform(kernelNames);
67
    else if (!platform->supportsKernels(kernelNames))
68
        throw OpenMMException("Specified a Platform for a Context which does not support all required kernels");
69
    platform->contextCreated(*this);
70
71
    initializeForcesKernel = platform->createKernel(InitializeForcesKernel::Name(), *this);
    dynamic_cast<InitializeForcesKernel&>(initializeForcesKernel.getImpl()).initialize(system);
72
    kineticEnergyKernel = platform->createKernel(CalcKineticEnergyKernel::Name(), *this);
73
    dynamic_cast<CalcKineticEnergyKernel&>(kineticEnergyKernel.getImpl()).initialize(system);
74
75
    updateTimeKernel = platform->createKernel(UpdateTimeKernel::Name(), *this);
    dynamic_cast<UpdateTimeKernel&>(updateTimeKernel.getImpl()).initialize(system);
76
    for (size_t i = 0; i < forceImpls.size(); ++i)
77
        forceImpls[i]->initialize(*this);
78
    integrator.initialize(*this);
Peter Eastman's avatar
Peter Eastman committed
79
80
81
    positions = platform->createStream("particlePositions", system.getNumParticles(), Stream::Double3, *this);
    velocities = platform->createStream("particleVelocities", system.getNumParticles(), Stream::Double3, *this);
    forces = platform->createStream("particleForces", system.getNumParticles(), Stream::Double3, *this);
82
83
    double zero[] = {0.0, 0.0, 0.0};
    velocities.fillWithValue(&zero);
84
85
}

86
ContextImpl::~ContextImpl() {
87
88
    for (int i = 0; i < (int) forceImpls.size(); ++i)
        delete forceImpls[i];
89
    platform->contextDestroyed(*this);
90
91
}

92
double ContextImpl::getTime() const {
93
94
95
    return dynamic_cast<const UpdateTimeKernel&>(updateTimeKernel.getImpl()).getTime(*this);
}

96
void ContextImpl::setTime(double t) {
97
98
99
    dynamic_cast<UpdateTimeKernel&>(updateTimeKernel.getImpl()).setTime(*this, t);
}

100
double ContextImpl::getParameter(std::string name) {
101
102
103
104
105
    if (parameters.find(name) == parameters.end())
        throw OpenMMException("Called getParameter() with invalid parameter name");
    return parameters[name];
}

106
void ContextImpl::setParameter(std::string name, double value) {
107
108
109
110
111
    if (parameters.find(name) == parameters.end())
        throw OpenMMException("Called setParameter() with invalid parameter name");
    parameters[name] = value;
}

112
void ContextImpl::calcForces() {
113
    dynamic_cast<InitializeForcesKernel&>(initializeForcesKernel.getImpl()).execute(*this);
114
115
116
117
    for (int i = 0; i < (int) forceImpls.size(); ++i)
        forceImpls[i]->calcForces(*this, forces);
}

118
double ContextImpl::calcKineticEnergy() {
119
    return dynamic_cast<CalcKineticEnergyKernel&>(kineticEnergyKernel.getImpl()).execute(*this);
120
121
}

122
double ContextImpl::calcPotentialEnergy() {
123
124
125
126
127
128
    double energy = 0.0;
    for (int i = 0; i < (int) forceImpls.size(); ++i)
        energy += forceImpls[i]->calcEnergy(*this);
    return energy;
}

129
void ContextImpl::updateContextState() {
130
131
132
133
    for (int i = 0; i < (int) forceImpls.size(); ++i)
        forceImpls[i]->updateContextState(*this);
}

134
void* ContextImpl::getPlatformData() {
135
136
137
    return platformData;
}

138
139
140
141
const void* ContextImpl::getPlatformData() const {
    return platformData;
}

142
void ContextImpl::setPlatformData(void* data) {
143
144
    platformData = data;
}