CustomCVForceImpl.cpp 6.09 KB
Newer Older
peastman's avatar
peastman committed
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.                                        *
peastman's avatar
peastman committed
6
 *                                                                            *
7
 * Portions copyright (c) 2008-2025 Stanford University and the Authors.      *
peastman's avatar
peastman committed
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
 * 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 "openmm/NonbondedForce.h"
#include "openmm/OpenMMException.h"
#include "openmm/internal/ContextImpl.h"
#include "openmm/internal/CustomCVForceImpl.h"
#include "openmm/kernels.h"
#include "openmm/serialization/XmlSerializer.h"
#include <map>

using namespace OpenMM;
using namespace std;

CustomCVForceImpl::CustomCVForceImpl(const CustomCVForce& owner) : owner(owner), innerIntegrator(1.0),
        innerContext(NULL) {
43
    forceGroup = owner.getForceGroup();
peastman's avatar
peastman committed
44
45
46
47
48
49
50
51
52
53
54
}

CustomCVForceImpl::~CustomCVForceImpl() {
    if (innerContext != NULL)
        delete innerContext;
}

void CustomCVForceImpl::initialize(ContextImpl& context) {
    // Construct the inner system used to evaluate collective variables.
    
    const System& system = context.getSystem();
55
56
57
    Vec3 a, b, c;
    system.getDefaultPeriodicBoxVectors(a, b, c);
    innerSystem.setDefaultPeriodicBoxVectors(a, b, c);
peastman's avatar
peastman committed
58
59
60
61
62
63
64
65
66
67
68
69
70
    for (int i = 0; i < system.getNumParticles(); i++)
        innerSystem.addParticle(system.getParticleMass(i));
    for (int i = 0; i < owner.getNumCollectiveVariables(); i++) {
        Force* variable = XmlSerializer::clone<Force>(owner.getCollectiveVariable(i));
        variable->setForceGroup(i);
        NonbondedForce* nonbonded = dynamic_cast<NonbondedForce*>(variable);
        if (nonbonded != NULL)
            nonbonded->setReciprocalSpaceForceGroup(-1);
        innerSystem.addForce(variable);
    }
    
    // Create the inner context.
    
71
    innerContext = context.createLinkedContext(innerSystem, innerIntegrator);
72
73
    vector<Vec3> positions(system.getNumParticles(), Vec3());
    innerContext->setPositions(positions);
peastman's avatar
peastman committed
74
75
76
77
    
    // Create the kernel.
    
    kernel = context.getPlatform().createKernel(CalcCustomCVForceKernel::Name(), context);
78
    kernel.getAs<CalcCustomCVForceKernel>().initialize(context.getSystem(), owner, getContextImpl(*innerContext));
peastman's avatar
peastman committed
79
80
81
}

double CustomCVForceImpl::calcForcesAndEnergy(ContextImpl& context, bool includeForces, bool includeEnergy, int groups) {
82
    if ((groups&(1<<forceGroup)) != 0)
peastman's avatar
peastman committed
83
84
85
86
87
88
89
90
91
92
        return kernel.getAs<CalcCustomCVForceKernel>().execute(context, getContextImpl(*innerContext), includeForces, includeEnergy);
    return 0.0;
}

vector<string> CustomCVForceImpl::getKernelNames() {
    vector<string> names;
    names.push_back(CalcCustomCVForceKernel::Name());
    return names;
}

93
94
95
96
97
98
99
100
101
102
vector<pair<int, int> > CustomCVForceImpl::getBondedParticles() const {
    vector<pair<int, int> > bonds;
    const ContextImpl& innerContextImpl = getContextImpl(*innerContext);
    for (auto& impl : innerContextImpl.getForceImpls()) {
        for (auto& bond : impl->getBondedParticles())
            bonds.push_back(bond);
    }
    return bonds;
}

peastman's avatar
peastman committed
103
104
map<string, double> CustomCVForceImpl::getDefaultParameters() {
    map<string, double> parameters;
105
    parameters.insert(innerContext->getParameters().begin(), innerContext->getParameters().end());
peastman's avatar
peastman committed
106
107
108
109
110
111
112
113
114
    for (int i = 0; i < owner.getNumGlobalParameters(); i++)
        parameters[owner.getGlobalParameterName(i)] = owner.getGlobalParameterDefaultValue(i);
    return parameters;
}

void CustomCVForceImpl::getCollectiveVariableValues(ContextImpl& context, vector<double>& values) {
    kernel.getAs<CalcCustomCVForceKernel>().copyState(context, getContextImpl(*innerContext));
    values.clear();
    for (int i = 0; i < innerSystem.getNumForces(); i++) {
115
        double value = innerContext->getState(State::Energy, false, 1<<i).getPotentialEnergy();
peastman's avatar
peastman committed
116
117
118
        values.push_back(value);
    }
}
119
120
121
122

Context& CustomCVForceImpl::getInnerContext() {
    return *innerContext;
}
123
124
125
126
127

void CustomCVForceImpl::updateParametersInContext(ContextImpl& context) {
    kernel.getAs<CalcCustomCVForceKernel>().copyParametersToContext(context, owner);
    context.systemChanged();
}
128
129
130
131
132
133
134

vector<const Force*> CustomCVForceImpl::getContainedForces() const {
    vector<const Force*> forces;
    for (int i = 0; i < owner.getNumCollectiveVariables(); i++)
        forces.push_back(&owner.getCollectiveVariable(i));
    return forces;
}