AndersenThermostatImpl.cpp 5.18 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-2010 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
#include "openmm/internal/AndersenThermostatImpl.h"
33
#include "openmm/internal/ContextImpl.h"
34
35
36
#include "openmm/Integrator.h"
#include "openmm/System.h"
#include "openmm/kernels.h"
37
#include <vector>
38
39

using namespace OpenMM;
40
using std::vector;
41

42
43
44
AndersenThermostatImpl::AndersenThermostatImpl(AndersenThermostat& owner) : owner(owner) {
}

45
void AndersenThermostatImpl::initialize(ContextImpl& context) {
46
    kernel = context.getPlatform().createKernel(ApplyAndersenThermostatKernel::Name(), context);
47
    dynamic_cast<ApplyAndersenThermostatKernel&>(kernel.getImpl()).initialize(context.getSystem(), owner);
48
49
}

50
void AndersenThermostatImpl::updateContextState(ContextImpl& context) {
51
    dynamic_cast<ApplyAndersenThermostatKernel&>(kernel.getImpl()).execute(context);
52
53
54
55
}

std::map<std::string, double> AndersenThermostatImpl::getDefaultParameters() {
    std::map<std::string, double> parameters;
56
57
    parameters[AndersenThermostat::Temperature()] = getOwner().getDefaultTemperature();
    parameters[AndersenThermostat::CollisionFrequency()] = getOwner().getDefaultCollisionFrequency();
58
59
60
61
62
63
64
65
    return parameters;
}

std::vector<std::string> AndersenThermostatImpl::getKernelNames() {
    std::vector<std::string> names;
    names.push_back(ApplyAndersenThermostatKernel::Name());
    return names;
}
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

vector<vector<int> > AndersenThermostatImpl::calcParticleGroups(const System& system) {
    // First make a list of every other particle to which each particle is connected by a constraint.

    int numParticles = system.getNumParticles();
    vector<vector<int> > particleConstraints(numParticles);
    for (int i = 0; i < system.getNumConstraints(); i++) {
        int particle1, particle2;
        double distance;
        system.getConstraintParameters(i, particle1, particle2, distance);
        particleConstraints[particle1].push_back(particle2);
        particleConstraints[particle2].push_back(particle1);
    }

    // Now tag particles by which molecule they belong to.

    vector<int> particleGroup(numParticles, -1);
    int numGroups = 0;
    for (int i = 0; i < numParticles; i++)
        if (particleGroup[i] == -1)
            tagParticlesInGroup(i, numGroups++, particleGroup, particleConstraints);
    vector<vector<int> > particleIndices(numGroups);
    for (int i = 0; i < numParticles; i++)
        particleIndices[particleGroup[i]].push_back(i);
    return particleIndices;
}

void AndersenThermostatImpl::tagParticlesInGroup(int particle, int group, vector<int>& particleGroup, vector<vector<int> >& particleConstraints) {
    // Recursively tag particles as belonging to a particular group.

    particleGroup[particle] = group;
    for (int i = 0; i < (int) particleConstraints[particle].size(); i++)
        if (particleGroup[particleConstraints[particle][i]] == -1)
            tagParticlesInGroup(particleConstraints[particle][i], group, particleGroup, particleConstraints);
}