"platforms/cpu/src/CpuCustomNonbondedForceVec4.cpp" did not exist on "0671c7b00df6624163dc3e52268ad04338a9e4d1"
StandardMMForceFieldImpl.cpp 8.46 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
33
34
35
36
37
38
39
40
/* -------------------------------------------------------------------------- *
 *                                   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"
#include "internal/StandardMMForceFieldImpl.h"
#include "kernels.h"

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

41
42
43
44
45
46
47
StandardMMForceFieldImpl::StandardMMForceFieldImpl(StandardMMForceField& owner) : owner(owner) {
}

StandardMMForceFieldImpl::~StandardMMForceFieldImpl() {
}

void StandardMMForceFieldImpl::initialize(OpenMMContextImpl& context) {
48
    kernel = context.getPlatform().createKernel(CalcStandardMMForceFieldKernel::Name());
49
50
51
52
53
54
55
56
    vector<vector<int> > bondIndices(owner.getNumBonds());
    vector<vector<double> > bondParameters(owner.getNumBonds());
    vector<vector<int> > angleIndices(owner.getNumAngles());
    vector<vector<double> > angleParameters(owner.getNumAngles());
    vector<vector<int> > periodicTorsionIndices(owner.getNumPeriodicTorsions());
    vector<vector<double> > periodicTorsionParameters(owner.getNumPeriodicTorsions());
    vector<vector<int> > rbTorsionIndices(owner.getNumRBTorsions());
    vector<vector<double> > rbTorsionParameters(owner.getNumRBTorsions());
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    vector<vector<int> > bonded14Indices;
    vector<set<int> > exclusions(owner.getNumAtoms());
    vector<vector<double> > nonbondedParameters(owner.getNumAtoms());
    for (int i = 0; i < owner.getNumBonds(); ++i) {
        int atom1, atom2;
        double length, k;
        owner.getBondParameters(i, atom1, atom2, length, k);
        bondIndices[i].push_back(atom1);
        bondIndices[i].push_back(atom2);
        bondParameters[i].push_back(length);
        bondParameters[i].push_back(k);
    }
    for (int i = 0; i < owner.getNumAngles(); ++i) {
        int atom1, atom2, atom3;
        double angle, k;
        owner.getAngleParameters(i, atom1, atom2, atom3, angle, k);
        angleIndices[i].push_back(atom1);
        angleIndices[i].push_back(atom2);
        angleIndices[i].push_back(atom3);
        angleParameters[i].push_back(angle);
        angleParameters[i].push_back(k);
    }
    for (int i = 0; i < owner.getNumPeriodicTorsions(); ++i) {
        int atom1, atom2, atom3, atom4, periodicity;
        double phase, k;
        owner.getPeriodicTorsionParameters(i, atom1, atom2, atom3, atom4, periodicity, phase, k);
        periodicTorsionIndices[i].push_back(atom1);
        periodicTorsionIndices[i].push_back(atom2);
        periodicTorsionIndices[i].push_back(atom3);
        periodicTorsionIndices[i].push_back(atom4);
        periodicTorsionParameters[i].push_back(k);
88
89
        periodicTorsionParameters[i].push_back(phase);
        periodicTorsionParameters[i].push_back(periodicity);
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    }
    for (int i = 0; i < owner.getNumRBTorsions(); ++i) {
        int atom1, atom2, atom3, atom4;
        double c0, c1, c2, c3, c4, c5;
        owner.getRBTorsionParameters(i, atom1, atom2, atom3, atom4, c0, c1, c2, c3, c4, c5);
        rbTorsionIndices[i].push_back(atom1);
        rbTorsionIndices[i].push_back(atom2);
        rbTorsionIndices[i].push_back(atom3);
        rbTorsionIndices[i].push_back(atom4);
        rbTorsionParameters[i].push_back(c0);
        rbTorsionParameters[i].push_back(c1);
        rbTorsionParameters[i].push_back(c2);
        rbTorsionParameters[i].push_back(c3);
        rbTorsionParameters[i].push_back(c4);
        rbTorsionParameters[i].push_back(c5);
    }
    for (int i = 0; i < owner.getNumAtoms(); ++i) {
        double charge, radius, depth;
        owner.getAtomParameters(i, charge, radius, depth);
        nonbondedParameters[i].push_back(charge);
        nonbondedParameters[i].push_back(radius);
        nonbondedParameters[i].push_back(depth);
    }
    set<pair<int, int> > bonded14set;
    findExclusions(bondIndices, exclusions, bonded14set);
115
116
117
118
119
120
    bonded14Indices.resize(bonded14set.size());
    int index = 0;
    for (set<pair<int, int> >::const_iterator iter = bonded14set.begin(); iter != bonded14set.end(); ++iter) {
        bonded14Indices[index].push_back(iter->first);
        bonded14Indices[index++].push_back(iter->second);
    }
121
122
    dynamic_cast<CalcStandardMMForceFieldKernel&>(kernel.getImpl()).initialize(bondIndices, bondParameters, angleIndices, angleParameters,
            periodicTorsionIndices, periodicTorsionParameters, rbTorsionIndices, rbTorsionParameters, bonded14Indices, 0.5, 1.0/1.2, exclusions, nonbondedParameters);
123
124
125
}

void StandardMMForceFieldImpl::calcForces(OpenMMContextImpl& context, Stream& forces) {
126
    dynamic_cast<CalcStandardMMForceFieldKernel&>(kernel.getImpl()).executeForces(context.getPositions(), forces);
127
128
129
}

double StandardMMForceFieldImpl::calcEnergy(OpenMMContextImpl& context) {
130
    return dynamic_cast<CalcStandardMMForceFieldKernel&>(kernel.getImpl()).executeEnergy(context.getPositions());
131
132
133
134
}

std::vector<std::string> StandardMMForceFieldImpl::getKernelNames() {
    std::vector<std::string> names;
135
    names.push_back(CalcStandardMMForceFieldKernel::Name());
136
137
138
139
140
141
142
    return names;
}

void StandardMMForceFieldImpl::findExclusions(const vector<vector<int> >& bondIndices, vector<set<int> >& exclusions, set<pair<int, int> >& bonded14Indices) const {
    vector<set<int> > bonded12(exclusions.size());
    for (int i = 0; i < (int) bondIndices.size(); ++i) {
        bonded12[bondIndices[i][0]].insert(bondIndices[i][1]);
143
        bonded12[bondIndices[i][1]].insert(bondIndices[i][0]);
144
145
    }
    for (int i = 0; i < (int) exclusions.size(); ++i)
146
        addExclusionsToSet(bonded12, exclusions[i], i, i, 2);
147
148
    for (int i = 0; i < (int) exclusions.size(); ++i) {
        set<int> bonded13;
149
        addExclusionsToSet(bonded12, bonded13, i, i, 1);
150
151
152
153
154
155
        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));
    }
}

156
void StandardMMForceFieldImpl::addExclusionsToSet(const vector<set<int> >& bonded12, set<int>& exclusions, int baseAtom, int fromAtom, int currentLevel) const {
157
    for (set<int>::const_iterator iter = bonded12[fromAtom].begin(); iter != bonded12[fromAtom].end(); ++iter) {
158
159
        if (*iter != baseAtom)
            exclusions.insert(*iter);
160
        if (currentLevel > 0)
161
            addExclusionsToSet(bonded12, exclusions, baseAtom, *iter, currentLevel-1);
162
163
164
    }
}