ContextImpl.cpp 18.3 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-2013 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
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 "openmm/State.h"
40
#include "openmm/VirtualSite.h"
Peter Eastman's avatar
Peter Eastman committed
41
#include "openmm/Context.h"
42
#include <algorithm>
Peter Eastman's avatar
Peter Eastman committed
43
#include <iostream>
44
#include <map>
45
#include <utility>
46
47
48
#include <vector>

using namespace OpenMM;
Peter Eastman's avatar
Peter Eastman committed
49
using namespace std;
50

51
ContextImpl::ContextImpl(Context& owner, const System& system, Integrator& integrator, Platform* platform, const map<string, string>& properties) :
52
53
        owner(owner), system(system), integrator(integrator), hasInitializedForces(false), hasSetPositions(false), integratorIsDeleted(false),
        lastForceGroups(-1), platform(platform), platformData(NULL) {
54
55
    if (system.getNumParticles() == 0)
        throw OpenMMException("Cannot create a Context for a System with no particles");
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    
    // Check for errors in virtual sites and massless particles.
    
    for (int i = 0; i < system.getNumParticles(); i++) {
        if (system.isVirtualSite(i)) {
            if (system.getParticleMass(i) != 0.0)
                throw OpenMMException("Virtual site has nonzero mass");
            const VirtualSite& site = system.getVirtualSite(i);
            for (int j = 0; j < site.getNumParticles(); j++)
                if (system.isVirtualSite(site.getParticle(j)))
                    throw OpenMMException("A virtual site cannot depend on another virtual site");
        }
    }
    for (int i = 0; i < system.getNumConstraints(); i++) {
        int particle1, particle2;
        double distance;
        system.getConstraintParameters(i, particle1, particle2, distance);
73
74
75
        double mass1 = system.getParticleMass(particle1);
        double mass2 = system.getParticleMass(particle2);
        if ((mass1 == 0.0 && mass2 != 0.0) || (mass2 == 0.0 && mass1 != 0.0))
76
77
78
            throw OpenMMException("A constraint cannot involve a massless particle");
    }
    
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    // Validate the list of properties.

    const vector<string>& platformProperties = platform->getPropertyNames();
    for (map<string, string>::const_iterator iter = properties.begin(); iter != properties.end(); ++iter) {
        bool valid = false;
        for (int i = 0; i < (int) platformProperties.size(); i++)
            if (platformProperties[i] == iter->first) {
                valid = true;
                break;
            }
        if (!valid)
            throw OpenMMException("Illegal property name: "+iter->first);
    }
    
93
94
    // Find the list of kernels required.
    
95
    vector<string> kernelNames;
96
    kernelNames.push_back(CalcForcesAndEnergyKernel::Name());
97
    kernelNames.push_back(UpdateStateDataKernel::Name());
98
99
    kernelNames.push_back(ApplyConstraintsKernel::Name());
    kernelNames.push_back(VirtualSitesKernel::Name());
100
    for (int i = 0; i < system.getNumForces(); ++i) {
101
        forceImpls.push_back(system.getForce(i).createImpl());
102
103
        map<string, double> forceParameters = forceImpls[forceImpls.size()-1]->getDefaultParameters();
        parameters.insert(forceParameters.begin(), forceParameters.end());
104
105
106
        vector<string> forceKernels = forceImpls[forceImpls.size()-1]->getKernelNames();
        kernelNames.insert(kernelNames.begin(), forceKernels.begin(), forceKernels.end());
    }
107
    hasInitializedForces = true;
108
109
    vector<string> integratorKernels = integrator.getKernelNames();
    kernelNames.insert(kernelNames.begin(), integratorKernels.begin(), integratorKernels.end());
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    
    // Select a platform to use.
    
    vector<pair<double, Platform*> > candidatePlatforms;
    if (platform == NULL) {
        for (int i = 0; i < Platform::getNumPlatforms(); i++) {
            Platform& p = Platform::getPlatform(i);
            if (p.supportsKernels(kernelNames))
                candidatePlatforms.push_back(make_pair(p.getSpeed(), &p));
        }
        if (candidatePlatforms.size() == 0)
            throw OpenMMException("No Platform supports all the requested kernels");
        sort(candidatePlatforms.begin(), candidatePlatforms.end());
    }
    else {
        if (!platform->supportsKernels(kernelNames))
            throw OpenMMException("Specified a Platform for a Context which does not support all required kernels");
        candidatePlatforms.push_back(make_pair(platform->getSpeed(), platform));
    }
    for (int i = candidatePlatforms.size()-1; i >= 0; i--) {
        try {
            this->platform = platform = candidatePlatforms[i].second;
            platform->contextCreated(*this, properties);
            break;
        }
        catch (...) {
            if (i > 0)
                continue;
            throw;
        }
    }
141
142
143
    
    // Create and initialize kernels and other objects.
    
144
    initializeForcesKernel = platform->createKernel(CalcForcesAndEnergyKernel::Name(), *this);
145
    initializeForcesKernel.getAs<CalcForcesAndEnergyKernel>().initialize(system);
146
    updateStateDataKernel = platform->createKernel(UpdateStateDataKernel::Name(), *this);
147
    updateStateDataKernel.getAs<UpdateStateDataKernel>().initialize(system);
148
    applyConstraintsKernel = platform->createKernel(ApplyConstraintsKernel::Name(), *this);
149
    applyConstraintsKernel.getAs<ApplyConstraintsKernel>().initialize(system);
150
    virtualSitesKernel = platform->createKernel(VirtualSitesKernel::Name(), *this);
151
    virtualSitesKernel.getAs<VirtualSitesKernel>().initialize(system);
152
153
    Vec3 periodicBoxVectors[3];
    system.getDefaultPeriodicBoxVectors(periodicBoxVectors[0], periodicBoxVectors[1], periodicBoxVectors[2]);
154
    updateStateDataKernel.getAs<UpdateStateDataKernel>().setPeriodicBoxVectors(*this, periodicBoxVectors[0], periodicBoxVectors[1], periodicBoxVectors[2]);
155
    for (size_t i = 0; i < forceImpls.size(); ++i)
156
        forceImpls[i]->initialize(*this);
157
    integrator.initialize(*this);
158
    updateStateDataKernel.getAs<UpdateStateDataKernel>().setVelocities(*this, vector<Vec3>(system.getNumParticles()));
159
160
}

161
ContextImpl::~ContextImpl() {
162
163
    for (int i = 0; i < (int) forceImpls.size(); ++i)
        delete forceImpls[i];
164
165
166
167
168
169
170
    
    // Make sure all kernels get properly deleted before contextDestroyed() is called.
    
    initializeForcesKernel = Kernel();
    updateStateDataKernel = Kernel();
    applyConstraintsKernel = Kernel();
    virtualSitesKernel = Kernel();
171
172
173
174
175
176
    if (!integratorIsDeleted) {
        // The Context is being deleted before the Integrator, so call cleanup() on it now.
        
        integrator.cleanup();
        integrator.context = NULL;
    }
177
    platform->contextDestroyed(*this);
178
179
}

180
double ContextImpl::getTime() const {
181
    return updateStateDataKernel.getAs<const UpdateStateDataKernel>().getTime(*this);
182
183
}

184
void ContextImpl::setTime(double t) {
185
    updateStateDataKernel.getAs<UpdateStateDataKernel>().setTime(*this, t);
186
187
188
}

void ContextImpl::getPositions(std::vector<Vec3>& positions) {
189
    updateStateDataKernel.getAs<UpdateStateDataKernel>().getPositions(*this, positions);
190
191
192
}

void ContextImpl::setPositions(const std::vector<Vec3>& positions) {
193
    hasSetPositions = true;
194
    updateStateDataKernel.getAs<UpdateStateDataKernel>().setPositions(*this, positions);
195
    integrator.stateChanged(State::Positions);
196
197
198
}

void ContextImpl::getVelocities(std::vector<Vec3>& velocities) {
199
    updateStateDataKernel.getAs<UpdateStateDataKernel>().getVelocities(*this, velocities);
200
201
202
}

void ContextImpl::setVelocities(const std::vector<Vec3>& velocities) {
203
    updateStateDataKernel.getAs<UpdateStateDataKernel>().setVelocities(*this, velocities);
204
    integrator.stateChanged(State::Velocities);
205
206
207
}

void ContextImpl::getForces(std::vector<Vec3>& forces) {
208
    updateStateDataKernel.getAs<UpdateStateDataKernel>().getForces(*this, forces);
209
210
}

211
212
213
214
const std::map<std::string, double>& ContextImpl::getParameters() const {
    return parameters;
}

215
double ContextImpl::getParameter(std::string name) {
216
217
218
219
220
    if (parameters.find(name) == parameters.end())
        throw OpenMMException("Called getParameter() with invalid parameter name");
    return parameters[name];
}

221
void ContextImpl::setParameter(std::string name, double value) {
222
223
224
    if (parameters.find(name) == parameters.end())
        throw OpenMMException("Called setParameter() with invalid parameter name");
    parameters[name] = value;
225
    integrator.stateChanged(State::Parameters);
226
227
}

228
void ContextImpl::getPeriodicBoxVectors(Vec3& a, Vec3& b, Vec3& c) {
229
    updateStateDataKernel.getAs<UpdateStateDataKernel>().getPeriodicBoxVectors(*this, a, b, c);
230
231
232
233
234
235
236
237
238
}

void ContextImpl::setPeriodicBoxVectors(const Vec3& a, const Vec3& b, const 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.");
239
    updateStateDataKernel.getAs<UpdateStateDataKernel>().setPeriodicBoxVectors(*this, a, b, c);
240
241
}

242
void ContextImpl::applyConstraints(double tol) {
243
    applyConstraintsKernel.getAs<ApplyConstraintsKernel>().apply(*this, tol);
244
245
}

246
247
248
249
void ContextImpl::applyVelocityConstraints(double tol) {
    applyConstraintsKernel.getAs<ApplyConstraintsKernel>().applyToVelocities(*this, tol);
}

250
void ContextImpl::computeVirtualSites() {
251
    virtualSitesKernel.getAs<VirtualSitesKernel>().computePositions(*this);
252
253
}

254
double ContextImpl::calcForcesAndEnergy(bool includeForces, bool includeEnergy, int groups) {
255
256
    if (!hasSetPositions)
        throw OpenMMException("Particle positions have not been set");
257
    lastForceGroups = groups;
258
    CalcForcesAndEnergyKernel& kernel = initializeForcesKernel.getAs<CalcForcesAndEnergyKernel>();
259
    double energy = 0.0;
260
    kernel.beginComputation(*this, includeForces, includeEnergy, groups);
261
    for (int i = 0; i < (int) forceImpls.size(); ++i)
262
263
        energy += forceImpls[i]->calcForcesAndEnergy(*this, includeForces, includeEnergy, groups);
    energy += kernel.finishComputation(*this, includeForces, includeEnergy, groups);
264
    return energy;
265
266
}

267
268
269
270
int ContextImpl::getLastForceGroups() const {
    return lastForceGroups;
}

271
double ContextImpl::calcKineticEnergy() {
272
    return integrator.computeKineticEnergy();
273
274
}

275
void ContextImpl::updateContextState() {
276
277
278
279
    for (int i = 0; i < (int) forceImpls.size(); ++i)
        forceImpls[i]->updateContextState(*this);
}

280
281
282
283
const vector<ForceImpl*>& ContextImpl::getForceImpls() const {
    return forceImpls;
}

284
285
286
287
vector<ForceImpl*>& ContextImpl::getForceImpls() {
    return forceImpls;
}

288
void* ContextImpl::getPlatformData() {
289
290
291
    return platformData;
}

292
293
294
295
const void* ContextImpl::getPlatformData() const {
    return platformData;
}

296
void ContextImpl::setPlatformData(void* data) {
297
298
    platformData = data;
}
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318

const vector<vector<int> >& ContextImpl::getMolecules() const {
    if (!hasInitializedForces)
        throw OpenMMException("ContextImpl: getMolecules() cannot be called until all ForceImpls have been initialized");
    if (molecules.size() > 0 || system.getNumParticles() == 0)
        return molecules;

    // First make a list of bonds and constraints.

    vector<pair<int, int> > bonds;
    for (int i = 0; i < system.getNumConstraints(); i++) {
        int particle1, particle2;
        double distance;
        system.getConstraintParameters(i, particle1, particle2, distance);
        bonds.push_back(std::make_pair(particle1, particle2));
    }
    for (int i = 0; i < (int) forceImpls.size(); i++) {
        vector<pair<int, int> > forceBonds = forceImpls[i]->getBondedParticles();
        bonds.insert(bonds.end(), forceBonds.begin(), forceBonds.end());
    }
319
320
321
322
323
324
325
    for (int i = 0; i < system.getNumParticles(); i++) {
        if (system.isVirtualSite(i)) {
            const VirtualSite& site = system.getVirtualSite(i);
            for (int j = 0; j < site.getNumParticles(); j++)
                bonds.push_back(std::make_pair(i, site.getParticle(j)));
        }
    }
326
327
328
329
330
331
332
333
334
335

    // Make a list of every other particle to which each particle is connected

    int numParticles = system.getNumParticles();
    vector<vector<int> > particleBonds(numParticles);
    for (int i = 0; i < (int) bonds.size(); i++) {
        particleBonds[bonds[i].first].push_back(bonds[i].second);
        particleBonds[bonds[i].second].push_back(bonds[i].first);
    }

336
    // Now identify particles by which molecule they belong to.
337

338
339
340
341
342
343
344
345
346
    molecules = findMolecules(numParticles, particleBonds);
    return molecules;
}

vector<vector<int> > ContextImpl::findMolecules(int numParticles, vector<vector<int> >& particleBonds) {
    // This is essentially a recursive algorithm, but it is reformulated as a loop to avoid
    // stack overflows.  It selects a particle, marks it as a new molecule, then recursively
    // marks every particle bonded to it as also being in that molecule.
    
347
348
349
    vector<int> particleMolecule(numParticles, -1);
    int numMolecules = 0;
    for (int i = 0; i < numParticles; i++)
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
        if (particleMolecule[i] == -1) {
            // Start a new molecule.
            
            vector<int> particleStack;
            vector<int> neighborStack;
            particleStack.push_back(i);
            neighborStack.push_back(0);
            int molecule = numMolecules++;
            
            // Recursively tag all the bonded particles.
            
            while (particleStack.size() > 0) {
                int particle = particleStack.back();
                particleMolecule[particle] = molecule;
                int& neighbor = neighborStack.back();
                while (neighbor < particleBonds[particle].size() && particleMolecule[particleBonds[particle][neighbor]] != -1)
                    neighbor++;
                if (neighbor < particleBonds[particle].size()) {
                    particleStack.push_back(particleBonds[particle][neighbor]);
                    neighborStack.push_back(0);
                }
                else {
                    particleStack.pop_back();
                    neighborStack.pop_back();
                }
            }
        }
    
    // Build the final output vector.
    
    vector<vector<int> > molecules(numMolecules);
381
382
383
384
385
    for (int i = 0; i < numParticles; i++)
        molecules[particleMolecule[i]].push_back(i);
    return molecules;
}

Peter Eastman's avatar
Peter Eastman committed
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
static void writeString(ostream& stream, string str) {
    int length = str.size();
    stream.write((char*) &length, sizeof(int));
    stream.write((char*) &str[0], length);
}

static string readString(istream& stream) {
    int length;
    stream.read((char*) &length, sizeof(int));
    string str(length, ' ');
    stream.read((char*) &str[0], length);
    return str;
}

void ContextImpl::createCheckpoint(ostream& stream) {
    writeString(stream, getPlatform().getName());
    int numParticles = getSystem().getNumParticles();
    stream.write((char*) &numParticles, sizeof(int));
    int numParameters = parameters.size();
    stream.write((char*) &numParameters, sizeof(int));
    for (map<string, double>::const_iterator iter = parameters.begin(); iter != parameters.end(); ++iter) {
        writeString(stream, iter->first);
        stream.write((char*) &iter->second, sizeof(double));
    }
410
    updateStateDataKernel.getAs<UpdateStateDataKernel>().createCheckpoint(*this, stream);
Peter Eastman's avatar
Peter Eastman committed
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
    stream.flush();
}

void ContextImpl::loadCheckpoint(istream& stream) {
    string platformName = readString(stream);
    if (platformName != getPlatform().getName())
        throw OpenMMException("loadCheckpoint: Checkpoint was created with a different Platform: "+platformName);
    int numParticles;
    stream.read((char*) &numParticles, sizeof(int));
    if (numParticles != getSystem().getNumParticles())
        throw OpenMMException("loadCheckpoint: Checkpoint contains the wrong number of particles");
    int numParameters;
    stream.read((char*) &numParameters, sizeof(int));
    for (int i = 0; i < numParameters; i++) {
        string name = readString(stream);
        double value;
        stream.read((char*) &value, sizeof(double));
        parameters[name] = value;
    }
430
    updateStateDataKernel.getAs<UpdateStateDataKernel>().loadCheckpoint(*this, stream);
Peter Eastman's avatar
Peter Eastman committed
431
}