GBSAOBCForceImpl.cpp 4.37 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
31
#include "openmm/OpenMMException.h"
#include "openmm/internal/GBSAOBCForceImpl.h"
32
#include "openmm/internal/ContextImpl.h"
33
#include "openmm/internal/Messages.h"
34
#include "openmm/kernels.h"
35
36
37
38
39
#include <vector>

using namespace OpenMM;
using std::vector;

40
GBSAOBCForceImpl::GBSAOBCForceImpl(const GBSAOBCForce& owner) : owner(owner) {
41
    forceGroup = owner.getForceGroup();
42
43
}

44
void GBSAOBCForceImpl::initialize(ContextImpl& context) {
45
    kernel = context.getPlatform().createKernel(CalcGBSAOBCForceKernel::Name(), context);
46
47
    if (owner.getNumParticles() != context.getSystem().getNumParticles())
        throw OpenMMException("GBSAOBCForce must have exactly as many particles as the System it belongs to.");
48
49
    if (owner.getNonbondedMethod() == GBSAOBCForce::CutoffPeriodic) {
        Vec3 boxVectors[3];
50
        context.getSystem().getDefaultPeriodicBoxVectors(boxVectors[0], boxVectors[1], boxVectors[2]);
51
52
        double cutoff = owner.getCutoffDistance();
        if (cutoff > 0.5*boxVectors[0][0] || cutoff > 0.5*boxVectors[1][1] || cutoff > 0.5*boxVectors[2][2])
53
            throw OpenMMException("GBSAOBCForce: "+Messages::cutoffTooLarge);
54
    }
55
56
57
58
59
60
61
62
    for (int i = 0; i < owner.getNumParticles(); i++) {
        double charge, radius, scalingFactor;
        owner.getParticleParameters(i, charge, radius, scalingFactor);
        if (radius <= 0)
            throw OpenMMException("GBSAOBCForce: particle radius must be positive");
        if (scalingFactor <= 0)
            throw OpenMMException("GBSAOBCForce: particle scaling factor must be positive");
    }
63
    kernel.getAs<CalcGBSAOBCForceKernel>().initialize(context.getSystem(), owner);
64
65
}

66
double GBSAOBCForceImpl::calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy, int groups) {
67
    if ((groups&(1<<forceGroup)) != 0)
68
        return kernel.getAs<CalcGBSAOBCForceKernel>().execute(context, includeForces, includeEnergy);
Peter Eastman's avatar
Peter Eastman committed
69
    return 0.0;
70
71
}

72
std::vector<std::string> GBSAOBCForceImpl::getKernelNames() {
Peter Eastman's avatar
Peter Eastman committed
73
    return {CalcGBSAOBCForceKernel::Name()};
74
}
75
76
77

void GBSAOBCForceImpl::updateParametersInContext(ContextImpl& context) {
    kernel.getAs<CalcGBSAOBCForceKernel>().copyParametersToContext(context, owner);
78
    context.systemChanged();
79
}