"...serialization/tests/TestSerializeAmoebaBondForce.cpp" did not exist on "12daada5240b1375f19a8b3f41f6ee22742e759d"
DrudeForceImpl.cpp 6.53 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) 2013-2021 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 * 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.                                     *
 * -------------------------------------------------------------------------- */

#ifdef WIN32
  #define _USE_MATH_DEFINES // Needed to get M_PI
#endif
#include "openmm/OpenMMException.h"
#include "openmm/internal/ContextImpl.h"
#include "openmm/internal/DrudeForceImpl.h"
#include "openmm/DrudeKernels.h"
#include <cmath>
#include <map>
#include <set>
#include <sstream>

using namespace OpenMM;
using namespace std;

DrudeForceImpl::DrudeForceImpl(const DrudeForce& owner) : owner(owner) {
}

DrudeForceImpl::~DrudeForceImpl() {
}

void DrudeForceImpl::initialize(ContextImpl& context) {
    kernel = context.getPlatform().createKernel(CalcDrudeForceKernel::Name(), context);
    const System& system = context.getSystem();

    // Check for errors in the specification of particles.

    set<int> usedParticles;
    for (int i = 0; i < owner.getNumParticles(); i++) {
61
        int particle[5];
62
        double charge, k, k2, k3;
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
        owner.getParticleParameters(i, particle[0], particle[1], particle[2], particle[3], particle[4], charge, k, k2, k3);
        for (int i = 0; i < 2; i++) {
            if (particle[i] < 0 || particle[i] >= system.getNumParticles()) {
                stringstream msg;
                msg << "DrudeForce: Illegal particle index: ";
                msg << particle[i];
                throw OpenMMException(msg.str());
            }
            if (usedParticles.find(particle[i]) != usedParticles.end()) {
                stringstream msg;
                msg << "DrudeForce: Particle index is used by two different Drude particles: ";
                msg << particle[i];
                throw OpenMMException(msg.str());
            }
            usedParticles.insert(particle[i]);
78
        }
79
80
81
82
83
84
85
        for (int i = 2; i < 5; i++) {
            if (particle[i] < -1 || particle[i] >= system.getNumParticles()) {
                stringstream msg;
                msg << "DrudeForce: Illegal particle index: ";
                msg << particle[i];
                throw OpenMMException(msg.str());
            }
86
87
88
89
90
91
92
        }
    }

    // Check for errors in the specification of screened pairs.

    vector<set<int> > screenedPairs(owner.getNumParticles());
    for (int i = 0; i < owner.getNumScreenedPairs(); i++) {
93
        int particle[2];
94
        double thole;
95
96
97
98
99
100
101
102
        owner.getScreenedPairParameters(i, particle[0], particle[1], thole);
        for (int i = 0; i < 2; i++) {
            if (particle[i] < 0 || particle[i] >= owner.getNumParticles()) {
                stringstream msg;
                msg << "DrudeForce: Illegal particle index for a screened pair: ";
                msg << particle[i];
                throw OpenMMException(msg.str());
            }
103
        }
104
        if (screenedPairs[particle[0]].count(particle[1]) > 0 || screenedPairs[particle[1]].count(particle[0]) > 0) {
105
106
            stringstream msg;
            msg << "DrudeForce: Multiple screened pairs are specified for particles ";
107
            msg << particle[0];
108
            msg << " and ";
109
            msg << particle[1];
110
111
            throw OpenMMException(msg.str());
        }
112
113
        screenedPairs[particle[0]].insert(particle[1]);
        screenedPairs[particle[1]].insert(particle[0]);
114
115
116
117
118
    }
    kernel.getAs<CalcDrudeForceKernel>().initialize(context.getSystem(), owner);
}

double DrudeForceImpl::calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy, int groups) {
119
120
    if ((groups&(1<<owner.getForceGroup())) != 0)
        return kernel.getAs<CalcDrudeForceKernel>().execute(context, includeForces, includeEnergy);
peastman's avatar
peastman committed
121
    return 0.0;
122
123
}

124
125
vector<string> DrudeForceImpl::getKernelNames() {
    vector<string> names;
126
127
128
129
130
131
    names.push_back(CalcDrudeForceKernel::Name());
    return names;
}

void DrudeForceImpl::updateParametersInContext(ContextImpl& context) {
    kernel.getAs<CalcDrudeForceKernel>().copyParametersToContext(context, owner);
132
    context.systemChanged();
133
}
134
135
136
137
138
139
140
141
142
143
144

vector<pair<int, int> > DrudeForceImpl::getBondedParticles() const {
    int numParticles = owner.getNumParticles();
    vector<pair<int, int> > bonds(numParticles);
    for (int i = 0; i < numParticles; i++) {
        int p2, p3, p4;
        double charge, polarizability, aniso12, aniso34;
        owner.getParticleParameters(i, bonds[i].first, bonds[i].second, p2, p3, p4, charge, polarizability, aniso12, aniso34);
    }
    return bonds;
}