CustomNonbondedForceProxy.cpp 10.5 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-2016 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", 2);
46
    const CustomNonbondedForce& force = *reinterpret_cast<const CustomNonbondedForce*>(object);
47
    node.setIntProperty("forceGroup", force.getForceGroup());
48
49
50
    node.setStringProperty("energy", force.getEnergyFunction());
    node.setIntProperty("method", (int) force.getNonbondedMethod());
    node.setDoubleProperty("cutoff", force.getCutoffDistance());
51
52
53
    node.setBoolProperty("useSwitchingFunction", force.getUseSwitchingFunction());
    node.setDoubleProperty("switchingDistance", force.getSwitchingDistance());
    node.setBoolProperty("useLongRangeCorrection", force.getUseLongRangeCorrection());
54
55
56
57
58
59
60
61
    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));
    }
62
63
64
65
    SerializationNode& energyDerivs = node.createChildNode("EnergyParameterDerivatives");
    for (int i = 0; i < force.getNumEnergyParameterDerivatives(); i++) {
        energyDerivs.createChildNode("Parameter").setStringProperty("name", force.getEnergyParameterDerivativeName(i));
    }
66
67
68
69
    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
70
        SerializationNode& particle = particles.createChildNode("Particle");
71
72
73
74
        for (int j = 0; j < (int) params.size(); j++) {
            stringstream key;
            key << "param";
            key << j+1;
Peter Eastman's avatar
Peter Eastman committed
75
            particle.setDoubleProperty(key.str(), params[j]);
76
77
78
79
80
81
82
83
84
        }
    }
    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");
85
86
    for (int i = 0; i < force.getNumTabulatedFunctions(); i++)
        functions.createChildNode("Function", &force.getTabulatedFunction(i)).setStringProperty("name", force.getTabulatedFunctionName(i));
87
88
89

    SerializationNode& interactionGroups = node.createChildNode("InteractionGroups");
    for (int i = 0; i < force.getNumInteractionGroups(); i++) {
90
91
92
93
94
        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
95
96
        for (int p : set1)
            set1node.createChildNode("Particle").setIntProperty("index", p);
97
        SerializationNode& set2node = interactionGroup.createChildNode("Set2");
peastman's avatar
peastman committed
98
99
        for (int p : set2)
            set2node.createChildNode("Particle").setIntProperty("index", p);
100
    }
101
102
103
}

void* CustomNonbondedForceProxy::deserialize(const SerializationNode& node) const {
104
105
    int version = node.getIntProperty("version");
    if (version < 1 || version > 2)
106
107
108
109
        throw OpenMMException("Unsupported version number");
    CustomNonbondedForce* force = NULL;
    try {
        CustomNonbondedForce* force = new CustomNonbondedForce(node.getStringProperty("energy"));
110
        force->setForceGroup(node.getIntProperty("forceGroup", 0));
111
        force->setNonbondedMethod((CustomNonbondedForce::NonbondedMethod) node.getIntProperty("method"));
112
        force->setCutoffDistance(node.getDoubleProperty("cutoff"));
113
114
115
        force->setUseSwitchingFunction(node.getBoolProperty("useSwitchingFunction", false));
        force->setSwitchingDistance(node.getDoubleProperty("switchingDistance", -1.0));
        force->setUseLongRangeCorrection(node.getBoolProperty("useLongRangeCorrection", false));
116
        const SerializationNode& perParticleParams = node.getChildNode("PerParticleParameters");
peastman's avatar
peastman committed
117
        for (auto& parameter : perParticleParams.getChildren())
118
119
            force->addPerParticleParameter(parameter.getStringProperty("name"));
        const SerializationNode& globalParams = node.getChildNode("GlobalParameters");
peastman's avatar
peastman committed
120
        for (auto& parameter : globalParams.getChildren())
121
            force->addGlobalParameter(parameter.getStringProperty("name"), parameter.getDoubleProperty("default"));
122
123
        if (version > 1) {
            const SerializationNode& energyDerivs = node.getChildNode("EnergyParameterDerivatives");
peastman's avatar
peastman committed
124
            for (auto& parameter : energyDerivs.getChildren())
125
126
                force->addEnergyParameterDerivative(parameter.getStringProperty("name"));
        }
127
128
        const SerializationNode& particles = node.getChildNode("Particles");
        vector<double> params(force->getNumPerParticleParameters());
peastman's avatar
peastman committed
129
        for (auto& particle : particles.getChildren()) {
130
131
132
133
134
135
136
137
138
            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
139
        for (auto& exclusion : exclusions.getChildren())
140
141
            force->addExclusion(exclusion.getIntProperty("p1"), exclusion.getIntProperty("p2"));
        const SerializationNode& functions = node.getChildNode("Functions");
peastman's avatar
peastman committed
142
        for (auto& function : functions.getChildren()) {
143
144
145
146
147
            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
148

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