NonbondedForceImpl.cpp 5.72 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
/* -------------------------------------------------------------------------- *
 *                                   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 "internal/OpenMMContextImpl.h"
33
#include "internal/NonbondedForceImpl.h"
34
35
36
37
38
39
40
#include "kernels.h"

using namespace OpenMM;
using std::pair;
using std::vector;
using std::set;

41
NonbondedForceImpl::NonbondedForceImpl(NonbondedForce& owner) : owner(owner) {
42
43
}

44
NonbondedForceImpl::~NonbondedForceImpl() {
45
46
}

47
48
49
50
51
52
void NonbondedForceImpl::initialize(OpenMMContextImpl& context) {
    kernel = context.getPlatform().createKernel(CalcNonbondedForceKernel::Name(), context);
    
    // See if the system contains a HarmonicBondForce.  If so, use it to identify exclusions.
    
    System& system = context.getSystem();
Peter Eastman's avatar
Peter Eastman committed
53
    vector<set<int> > exclusions(owner.getNumParticles());
54
55
56
57
58
59
    for (int i = 0; i < system.getNumForces(); i++) {
        if (dynamic_cast<HarmonicBondForce*>(&system.getForce(i)) != NULL) {
            const HarmonicBondForce& force = dynamic_cast<const HarmonicBondForce&>(system.getForce(i));
            vector<vector<int> > bondIndices(force.getNumBonds());
            set<pair<int, int> > bonded14set;
            for (int i = 0; i < force.getNumBonds(); ++i) {
Peter Eastman's avatar
Peter Eastman committed
60
                int particle1, particle2;
61
                double length, k;
Peter Eastman's avatar
Peter Eastman committed
62
63
64
                force.getBondParameters(i, particle1, particle2, length, k);
                bondIndices[i].push_back(particle1);
                bondIndices[i].push_back(particle2);
65
66
67
            }
            findExclusions(bondIndices, exclusions, bonded14set);
        }
68
    }
Peter Eastman's avatar
Peter Eastman committed
69
    dynamic_cast<CalcNonbondedForceKernel&>(kernel.getImpl()).initialize(context.getSystem(), owner, exclusions);
70
71
}

72
73
void NonbondedForceImpl::calcForces(OpenMMContextImpl& context, Stream& forces) {
    dynamic_cast<CalcNonbondedForceKernel&>(kernel.getImpl()).executeForces(context);
74
75
}

76
77
double NonbondedForceImpl::calcEnergy(OpenMMContextImpl& context) {
    return dynamic_cast<CalcNonbondedForceKernel&>(kernel.getImpl()).executeEnergy(context);
78
79
}

80
std::vector<std::string> NonbondedForceImpl::getKernelNames() {
81
    std::vector<std::string> names;
82
    names.push_back(CalcNonbondedForceKernel::Name());
83
84
85
    return names;
}

86
void NonbondedForceImpl::findExclusions(const vector<vector<int> >& bondIndices, vector<set<int> >& exclusions, set<pair<int, int> >& bonded14Indices) const {
87
88
89
    vector<set<int> > bonded12(exclusions.size());
    for (int i = 0; i < (int) bondIndices.size(); ++i) {
        bonded12[bondIndices[i][0]].insert(bondIndices[i][1]);
90
        bonded12[bondIndices[i][1]].insert(bondIndices[i][0]);
91
92
    }
    for (int i = 0; i < (int) exclusions.size(); ++i)
93
        addExclusionsToSet(bonded12, exclusions[i], i, i, 2);
94
95
    for (int i = 0; i < (int) exclusions.size(); ++i) {
        set<int> bonded13;
96
        addExclusionsToSet(bonded12, bonded13, i, i, 1);
97
98
99
100
101
102
        for (set<int>::const_iterator iter = exclusions[i].begin(); iter != exclusions[i].end(); ++iter)
            if (*iter < i && bonded13.find(*iter) == bonded13.end())
                bonded14Indices.insert(pair<int, int> (*iter, i));
    }
}

Peter Eastman's avatar
Peter Eastman committed
103
104
105
void NonbondedForceImpl::addExclusionsToSet(const vector<set<int> >& bonded12, set<int>& exclusions, int baseParticle, int fromParticle, int currentLevel) const {
    for (set<int>::const_iterator iter = bonded12[fromParticle].begin(); iter != bonded12[fromParticle].end(); ++iter) {
        if (*iter != baseParticle)
106
            exclusions.insert(*iter);
107
        if (currentLevel > 0)
Peter Eastman's avatar
Peter Eastman committed
108
            addExclusionsToSet(bonded12, exclusions, baseParticle, *iter, currentLevel-1);
109
110
111
    }
}