DrudeForceImpl.cpp 6.32 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) 2013-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
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
 * 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++) {
59
        int particle[5];
60
        double charge, k, k2, k3;
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
        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]);
76
        }
77
78
79
80
81
82
83
        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());
            }
84
85
86
87
88
89
90
        }
    }

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

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

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

122
vector<string> DrudeForceImpl::getKernelNames() {
Peter Eastman's avatar
Peter Eastman committed
123
    return {CalcDrudeForceKernel::Name()};
124
125
126
127
}

void DrudeForceImpl::updateParametersInContext(ContextImpl& context) {
    kernel.getAs<CalcDrudeForceKernel>().copyParametersToContext(context, owner);
128
    context.systemChanged();
129
}
130
131
132
133
134
135
136
137
138
139
140

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;
}