AndersenThermostatImpl.cpp 5.13 KB
Newer Older
1
2
3
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
Evan Pretti's avatar
Evan Pretti committed
4
5
 * This is part of the OpenMM molecular simulation toolkit.                   *
 * See https://openmm.org/development.                                        *
6
 *                                                                            *
7
 * Portions copyright (c) 2008-2021 Stanford University and the Authors.      *
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * 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.                                     *
 * -------------------------------------------------------------------------- */

30
#include "openmm/internal/AndersenThermostatImpl.h"
31
#include "openmm/internal/ContextImpl.h"
32
#include "openmm/Integrator.h"
33
#include "openmm/OpenMMException.h"
34
35
#include "openmm/System.h"
#include "openmm/kernels.h"
36
#include <vector>
37
38

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

41
AndersenThermostatImpl::AndersenThermostatImpl(const AndersenThermostat& owner) : owner(owner) {
42
43
}

44
void AndersenThermostatImpl::initialize(ContextImpl& context) {
45
46
47
48
    if (owner.getDefaultTemperature() < 0)
        throw OpenMMException("AndersenThermostat: temperature cannot be negative");
    if (owner.getDefaultCollisionFrequency() < 0)
        throw OpenMMException("AndersenThermostat: collision frequency cannot be negative");
49
    kernel = context.getPlatform().createKernel(ApplyAndersenThermostatKernel::Name(), context);
50
    kernel.getAs<ApplyAndersenThermostatKernel>().initialize(context.getSystem(), owner);
51
52
}

53
void AndersenThermostatImpl::updateContextState(ContextImpl& context, bool& forcesInvalid) {
54
    kernel.getAs<ApplyAndersenThermostatKernel>().execute(context);
55
56
57
}

std::map<std::string, double> AndersenThermostatImpl::getDefaultParameters() {
Peter Eastman's avatar
Peter Eastman committed
58
59
    return {{AndersenThermostat::Temperature(), getOwner().getDefaultTemperature()},
            {AndersenThermostat::CollisionFrequency(), getOwner().getDefaultCollisionFrequency()}};
60
61
62
}

std::vector<std::string> AndersenThermostatImpl::getKernelNames() {
Peter Eastman's avatar
Peter Eastman committed
63
    return {ApplyAndersenThermostatKernel::Name()};
64
}
65
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

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;
peastman's avatar
peastman committed
96
97
98
    for (int constrained : particleConstraints[particle])
        if (particleGroup[constrained] == -1)
            tagParticlesInGroup(constrained, group, particleGroup, particleConstraints);
99
}