CustomNonbondedForceProxy.cpp 11.3 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) 2010-2022 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
 * 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/serialization/CustomNonbondedForceProxy.h"
#include "openmm/serialization/SerializationNode.h"
#include "openmm/Force.h"
#include "openmm/CustomNonbondedForce.h"
#include <sstream>

using namespace OpenMM;
using namespace std;

CustomNonbondedForceProxy::CustomNonbondedForceProxy() : SerializationProxy("CustomNonbondedForce") {
}

void CustomNonbondedForceProxy::serialize(const void* object, SerializationNode& node) const {
45
    node.setIntProperty("version", 3);
46
    const CustomNonbondedForce& force = *reinterpret_cast<const CustomNonbondedForce*>(object);
47
    node.setIntProperty("forceGroup", force.getForceGroup());
48
    node.setStringProperty("name", force.getName());
49
50
51
    node.setStringProperty("energy", force.getEnergyFunction());
    node.setIntProperty("method", (int) force.getNonbondedMethod());
    node.setDoubleProperty("cutoff", force.getCutoffDistance());
52
53
54
    node.setBoolProperty("useSwitchingFunction", force.getUseSwitchingFunction());
    node.setDoubleProperty("switchingDistance", force.getSwitchingDistance());
    node.setBoolProperty("useLongRangeCorrection", force.getUseLongRangeCorrection());
55
56
57
58
59
60
61
62
    SerializationNode& perParticleParams = node.createChildNode("PerParticleParameters");
    for (int i = 0; i < force.getNumPerParticleParameters(); i++) {
        perParticleParams.createChildNode("Parameter").setStringProperty("name", force.getPerParticleParameterName(i));
    }
    SerializationNode& globalParams = node.createChildNode("GlobalParameters");
    for (int i = 0; i < force.getNumGlobalParameters(); i++) {
        globalParams.createChildNode("Parameter").setStringProperty("name", force.getGlobalParameterName(i)).setDoubleProperty("default", force.getGlobalParameterDefaultValue(i));
    }
63
64
65
66
67
68
    SerializationNode& computedValues = node.createChildNode("ComputedValues");
    for (int i = 0; i < force.getNumComputedValues(); i++) {
        string name, expression;
        force.getComputedValueParameters(i, name, expression);
        computedValues.createChildNode("Value").setStringProperty("name", name).setStringProperty("expression", expression);
    }
69
70
71
72
    SerializationNode& energyDerivs = node.createChildNode("EnergyParameterDerivatives");
    for (int i = 0; i < force.getNumEnergyParameterDerivatives(); i++) {
        energyDerivs.createChildNode("Parameter").setStringProperty("name", force.getEnergyParameterDerivativeName(i));
    }
73
74
75
76
    SerializationNode& particles = node.createChildNode("Particles");
    for (int i = 0; i < force.getNumParticles(); i++) {
        vector<double> params;
        force.getParticleParameters(i, params);
Peter Eastman's avatar
Peter Eastman committed
77
        SerializationNode& particle = particles.createChildNode("Particle");
78
79
80
81
        for (int j = 0; j < (int) params.size(); j++) {
            stringstream key;
            key << "param";
            key << j+1;
Peter Eastman's avatar
Peter Eastman committed
82
            particle.setDoubleProperty(key.str(), params[j]);
83
84
85
86
87
88
89
90
91
        }
    }
    SerializationNode& exclusions = node.createChildNode("Exclusions");
    for (int i = 0; i < force.getNumExclusions(); i++) {
        int particle1, particle2;
        force.getExclusionParticles(i, particle1, particle2);
        exclusions.createChildNode("Exclusion").setIntProperty("p1", particle1).setIntProperty("p2", particle2);
    }
    SerializationNode& functions = node.createChildNode("Functions");
92
93
    for (int i = 0; i < force.getNumTabulatedFunctions(); i++)
        functions.createChildNode("Function", &force.getTabulatedFunction(i)).setStringProperty("name", force.getTabulatedFunctionName(i));
94
95
96

    SerializationNode& interactionGroups = node.createChildNode("InteractionGroups");
    for (int i = 0; i < force.getNumInteractionGroups(); i++) {
97
98
99
100
101
        SerializationNode& interactionGroup = interactionGroups.createChildNode("InteractionGroup");
        std::set<int> set1;
        std::set<int> set2;
        force.getInteractionGroupParameters(i, set1, set2);
        SerializationNode& set1node = interactionGroup.createChildNode("Set1");
peastman's avatar
peastman committed
102
103
        for (int p : set1)
            set1node.createChildNode("Particle").setIntProperty("index", p);
104
        SerializationNode& set2node = interactionGroup.createChildNode("Set2");
peastman's avatar
peastman committed
105
106
        for (int p : set2)
            set2node.createChildNode("Particle").setIntProperty("index", p);
107
    }
108
109
110
}

void* CustomNonbondedForceProxy::deserialize(const SerializationNode& node) const {
111
    int version = node.getIntProperty("version");
112
    if (version < 1 || version > 3)
113
114
115
116
        throw OpenMMException("Unsupported version number");
    CustomNonbondedForce* force = NULL;
    try {
        CustomNonbondedForce* force = new CustomNonbondedForce(node.getStringProperty("energy"));
117
        force->setForceGroup(node.getIntProperty("forceGroup", 0));
118
        force->setName(node.getStringProperty("name", force->getName()));
119
        force->setNonbondedMethod((CustomNonbondedForce::NonbondedMethod) node.getIntProperty("method"));
120
        force->setCutoffDistance(node.getDoubleProperty("cutoff"));
121
122
123
        force->setUseSwitchingFunction(node.getBoolProperty("useSwitchingFunction", false));
        force->setSwitchingDistance(node.getDoubleProperty("switchingDistance", -1.0));
        force->setUseLongRangeCorrection(node.getBoolProperty("useLongRangeCorrection", false));
124
        const SerializationNode& perParticleParams = node.getChildNode("PerParticleParameters");
peastman's avatar
peastman committed
125
        for (auto& parameter : perParticleParams.getChildren())
126
127
            force->addPerParticleParameter(parameter.getStringProperty("name"));
        const SerializationNode& globalParams = node.getChildNode("GlobalParameters");
peastman's avatar
peastman committed
128
        for (auto& parameter : globalParams.getChildren())
129
            force->addGlobalParameter(parameter.getStringProperty("name"), parameter.getDoubleProperty("default"));
130
131
132
133
134
        if (version > 2) {
            const SerializationNode& computedValues = node.getChildNode("ComputedValues");
            for (auto& value : computedValues.getChildren())
                force->addComputedValue(value.getStringProperty("name"), value.getStringProperty("expression"));
        }
135
136
        if (version > 1) {
            const SerializationNode& energyDerivs = node.getChildNode("EnergyParameterDerivatives");
peastman's avatar
peastman committed
137
            for (auto& parameter : energyDerivs.getChildren())
138
139
                force->addEnergyParameterDerivative(parameter.getStringProperty("name"));
        }
140
141
        const SerializationNode& particles = node.getChildNode("Particles");
        vector<double> params(force->getNumPerParticleParameters());
peastman's avatar
peastman committed
142
        for (auto& particle : particles.getChildren()) {
143
144
145
146
147
148
149
150
151
            for (int j = 0; j < (int) params.size(); j++) {
                stringstream key;
                key << "param";
                key << j+1;
                params[j] = particle.getDoubleProperty(key.str());
            }
            force->addParticle(params);
        }
        const SerializationNode& exclusions = node.getChildNode("Exclusions");
peastman's avatar
peastman committed
152
        for (auto& exclusion : exclusions.getChildren())
153
154
            force->addExclusion(exclusion.getIntProperty("p1"), exclusion.getIntProperty("p2"));
        const SerializationNode& functions = node.getChildNode("Functions");
peastman's avatar
peastman committed
155
        for (auto& function : functions.getChildren()) {
156
157
158
159
160
            if (function.hasProperty("type")) {
                force->addTabulatedFunction(function.getStringProperty("name"), function.decodeObject<TabulatedFunction>());
            }
            else {
                // This is an old file created before TabulatedFunction existed.
John Chodera (MSKCC)'s avatar
John Chodera (MSKCC) committed
161

162
163
                const SerializationNode& valuesNode = function.getChildNode("Values");
                vector<double> values;
peastman's avatar
peastman committed
164
165
                for (auto& child : valuesNode.getChildren())
                    values.push_back(child.getDoubleProperty("v"));
166
167
                force->addTabulatedFunction(function.getStringProperty("name"), new Continuous1DFunction(values, function.getDoubleProperty("min"), function.getDoubleProperty("max")));
            }
168
        }
169
        bool hasInteractionGroups = false; // Older files will be missing this block.
peastman's avatar
peastman committed
170
171
        for (auto& child : node.getChildren())
            if (child.getName() == "InteractionGroups")
172
173
174
                hasInteractionGroups = true;
        if (hasInteractionGroups) {
            const SerializationNode& interactionGroups = node.getChildNode("InteractionGroups");
peastman's avatar
peastman committed
175
            for (auto& interactionGroup : interactionGroups.getChildren()) {
176
177
178
                // Get set 1.
                const SerializationNode& set1node = interactionGroup.getChildNode("Set1");
                std::set<int> set1;
peastman's avatar
peastman committed
179
180
                for (auto& child : set1node.getChildren())
                    set1.insert(child.getIntProperty("index"));
181
182
183
                // Get set 2.
                const SerializationNode& set2node = interactionGroup.getChildNode("Set2");
                std::set<int> set2;
peastman's avatar
peastman committed
184
185
                for (auto& child : set2node.getChildren())
                    set2.insert(child.getIntProperty("index"));
186
187
                force->addInteractionGroup(set1, set2);
            }
John Chodera (MSKCC)'s avatar
John Chodera (MSKCC) committed
188
        }
189
190
191
192
193
194
195
196
        return force;
    }
    catch (...) {
        if (force != NULL)
            delete force;
        throw;
    }
}