"platforms/reference/tests/TestReferenceDPDIntegrator.cpp" did not exist on "3c8adf0c910097854a151eaf55764b807b4732d8"
CustomCompoundBondForceProxy.cpp 8.14 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/CustomCompoundBondForceProxy.h"
#include "openmm/serialization/SerializationNode.h"
#include "openmm/Force.h"
#include "openmm/CustomCompoundBondForce.h"
#include <sstream>

using namespace OpenMM;
using namespace std;

CustomCompoundBondForceProxy::CustomCompoundBondForceProxy() : SerializationProxy("CustomCompoundBondForce") {
}

void CustomCompoundBondForceProxy::serialize(const void* object, SerializationNode& node) const {
45
    node.setIntProperty("version", 3);
46
    const CustomCompoundBondForce& force = *reinterpret_cast<const CustomCompoundBondForce*>(object);
47
    node.setIntProperty("forceGroup", force.getForceGroup());
48
    node.setBoolProperty("usesPeriodic", force.usesPeriodicBoundaryConditions());
49
50
51
52
53
54
55
56
57
58
    node.setIntProperty("particles", force.getNumParticlesPerBond());
    node.setStringProperty("energy", force.getEnergyFunction());
    SerializationNode& perBondParams = node.createChildNode("PerBondParameters");
    for (int i = 0; i < force.getNumPerBondParameters(); i++) {
        perBondParams.createChildNode("Parameter").setStringProperty("name", force.getPerBondParameterName(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));
    }
59
60
61
62
    SerializationNode& energyDerivs = node.createChildNode("EnergyParameterDerivatives");
    for (int i = 0; i < force.getNumEnergyParameterDerivatives(); i++) {
        energyDerivs.createChildNode("Parameter").setStringProperty("name", force.getEnergyParameterDerivativeName(i));
    }
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    SerializationNode& bonds = node.createChildNode("Bonds");
    for (int i = 0; i < force.getNumBonds(); i++) {
        vector<int> particles;
        vector<double> params;
        force.getBondParameters(i, particles, params);
        SerializationNode& node = bonds.createChildNode("Bond");
        for (int j = 0; j < (int) particles.size(); j++) {
            stringstream key;
            key << "p";
            key << j+1;
            node.setIntProperty(key.str(), particles[j]);
        }
        for (int j = 0; j < (int) params.size(); j++) {
            stringstream key;
            key << "param";
            key << j+1;
            node.setDoubleProperty(key.str(), params[j]);
        }
    }
82
83
84
    SerializationNode& functions = node.createChildNode("Functions");
    for (int i = 0; i < force.getNumTabulatedFunctions(); i++)
        functions.createChildNode("Function", &force.getTabulatedFunction(i)).setStringProperty("name", force.getTabulatedFunctionName(i));
85
86
87
}

void* CustomCompoundBondForceProxy::deserialize(const SerializationNode& node) const {
88
    int version = node.getIntProperty("version");
89
    if (version < 1 || version > 3)
90
91
92
93
        throw OpenMMException("Unsupported version number");
    CustomCompoundBondForce* force = NULL;
    try {
        CustomCompoundBondForce* force = new CustomCompoundBondForce(node.getIntProperty("particles"), node.getStringProperty("energy"));
94
        force->setForceGroup(node.getIntProperty("forceGroup", 0));
95
96
        if (version > 1)
            force->setUsesPeriodicBoundaryConditions(node.getBoolProperty("usesPeriodic"));
97
        const SerializationNode& perBondParams = node.getChildNode("PerBondParameters");
peastman's avatar
peastman committed
98
        for (auto& parameter : perBondParams.getChildren())
99
100
            force->addPerBondParameter(parameter.getStringProperty("name"));
        const SerializationNode& globalParams = node.getChildNode("GlobalParameters");
peastman's avatar
peastman committed
101
        for (auto& parameter : globalParams.getChildren())
102
            force->addGlobalParameter(parameter.getStringProperty("name"), parameter.getDoubleProperty("default"));
103
104
        if (version > 2) {
            const SerializationNode& energyDerivs = node.getChildNode("EnergyParameterDerivatives");
peastman's avatar
peastman committed
105
            for (auto& parameter : energyDerivs.getChildren())
106
107
                force->addEnergyParameterDerivative(parameter.getStringProperty("name"));
        }
108
109
110
        const SerializationNode& bonds = node.getChildNode("Bonds");
        vector<int> particles(force->getNumParticlesPerBond());
        vector<double> params(force->getNumPerBondParameters());
peastman's avatar
peastman committed
111
        for (auto& bond : bonds.getChildren()) {
112
113
114
115
116
117
118
119
120
121
122
123
124
125
            for (int j = 0; j < (int) particles.size(); j++) {
                stringstream key;
                key << "p";
                key << j+1;
                particles[j] = bond.getIntProperty(key.str());
            }
            for (int j = 0; j < (int) params.size(); j++) {
                stringstream key;
                key << "param";
                key << j+1;
                params[j] = bond.getDoubleProperty(key.str());
            }
            force->addBond(particles, params);
        }
126
        const SerializationNode& functions = node.getChildNode("Functions");
peastman's avatar
peastman committed
127
        for (auto& function : functions.getChildren()) {
128
129
130
131
132
133
134
135
            if (function.hasProperty("type")) {
                force->addTabulatedFunction(function.getStringProperty("name"), function.decodeObject<TabulatedFunction>());
            }
            else {
                // This is an old file created before TabulatedFunction existed.
                
                const SerializationNode& valuesNode = function.getChildNode("Values");
                vector<double> values;
peastman's avatar
peastman committed
136
137
                for (auto& child : valuesNode.getChildren())
                    values.push_back(child.getDoubleProperty("v"));
138
139
140
                force->addTabulatedFunction(function.getStringProperty("name"), new Continuous1DFunction(values, function.getDoubleProperty("min"), function.getDoubleProperty("max")));
            }
        }
141
142
143
144
145
146
147
148
        return force;
    }
    catch (...) {
        if (force != NULL)
            delete force;
        throw;
    }
}