TabulatedFunctionProxies.cpp 11.8 KB
Newer Older
1
2
3
4
5
6
7
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
43
44
45
46
47
48
49
50
51
/* -------------------------------------------------------------------------- *
 *                                   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.               *
 *                                                                            *
 * Portions copyright (c) 2014 Stanford University and the Authors.           *
 * 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/TabulatedFunctionProxies.h"
#include "openmm/serialization/SerializationNode.h"
#include "openmm/TabulatedFunction.h"
#include <sstream>

using namespace OpenMM;
using namespace std;

Continuous1DFunctionProxy::Continuous1DFunctionProxy() : SerializationProxy("Continuous1DFunction") {
}

void Continuous1DFunctionProxy::serialize(const void* object, SerializationNode& node) const {
    node.setIntProperty("version", 1);
    const Continuous1DFunction& function = *reinterpret_cast<const Continuous1DFunction*>(object);
    double min, max;
    vector<double> values;
    function.getFunctionParameters(values, min, max);
    node.setDoubleProperty("min", min);
    node.setDoubleProperty("max", max);
    SerializationNode& valuesNode = node.createChildNode("Values");
peastman's avatar
peastman committed
52
53
    for (auto v : values)
        valuesNode.createChildNode("Value").setDoubleProperty("v", v);
54
55
56
    bool periodic;
    function.getPeriodicityStatus(periodic);
    node.setBoolProperty("periodic", periodic);
57
58
59
60
61
62
63
}

void* Continuous1DFunctionProxy::deserialize(const SerializationNode& node) const {
    if (node.getIntProperty("version") != 1)
        throw OpenMMException("Unsupported version number");
    const SerializationNode& valuesNode = node.getChildNode("Values");
    vector<double> values;
peastman's avatar
peastman committed
64
65
    for (auto& child : valuesNode.getChildren())
        values.push_back(child.getDoubleProperty("v"));
66
    return new Continuous1DFunction(values, node.getDoubleProperty("min"), node.getDoubleProperty("max"), node.getBoolProperty("periodic"));
67
68
}

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
ContinuousPeriodic1DFunctionProxy::ContinuousPeriodic1DFunctionProxy() : SerializationProxy("ContinuousPeriodic1DFunction") {
}

void ContinuousPeriodic1DFunctionProxy::serialize(const void* object, SerializationNode& node) const {
    node.setIntProperty("version", 1);
    const ContinuousPeriodic1DFunction& function = *reinterpret_cast<const ContinuousPeriodic1DFunction*>(object);
    double min, max;
    vector<double> values;
    function.getFunctionParameters(values, min, max);
    node.setDoubleProperty("min", min);
    node.setDoubleProperty("max", max);
    SerializationNode& valuesNode = node.createChildNode("Values");
    for (auto v : values)
        valuesNode.createChildNode("Value").setDoubleProperty("v", v);
}

void* ContinuousPeriodic1DFunctionProxy::deserialize(const SerializationNode& node) const {
    if (node.getIntProperty("version") != 1)
        throw OpenMMException("Unsupported version number");
    const SerializationNode& valuesNode = node.getChildNode("Values");
    vector<double> values;
    for (auto& child : valuesNode.getChildren())
        values.push_back(child.getDoubleProperty("v"));
    return new ContinuousPeriodic1DFunction(values, node.getDoubleProperty("min"), node.getDoubleProperty("max"));
}

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
Continuous2DFunctionProxy::Continuous2DFunctionProxy() : SerializationProxy("Continuous2DFunction") {
}

void Continuous2DFunctionProxy::serialize(const void* object, SerializationNode& node) const {
    node.setIntProperty("version", 1);
    const Continuous2DFunction& function = *reinterpret_cast<const Continuous2DFunction*>(object);
    int xsize, ysize;
    double xmin, xmax, ymin, ymax;
    vector<double> values;
    function.getFunctionParameters(xsize, ysize, values, xmin, xmax, ymin, ymax);
    node.setDoubleProperty("xsize", xsize);
    node.setDoubleProperty("ysize", ysize);
    node.setDoubleProperty("xmin", xmin);
    node.setDoubleProperty("xmax", xmax);
    node.setDoubleProperty("ymin", ymin);
    node.setDoubleProperty("ymax", ymax);
    SerializationNode& valuesNode = node.createChildNode("Values");
peastman's avatar
peastman committed
112
113
    for (auto v : values)
        valuesNode.createChildNode("Value").setDoubleProperty("v", v);
114
115
116
117
118
119
120
}

void* Continuous2DFunctionProxy::deserialize(const SerializationNode& node) const {
    if (node.getIntProperty("version") != 1)
        throw OpenMMException("Unsupported version number");
    const SerializationNode& valuesNode = node.getChildNode("Values");
    vector<double> values;
peastman's avatar
peastman committed
121
122
    for (auto& child : valuesNode.getChildren())
        values.push_back(child.getDoubleProperty("v"));
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    return new Continuous2DFunction(node.getIntProperty("xsize"), node.getIntProperty("ysize"), values,
            node.getDoubleProperty("xmin"), node.getDoubleProperty("xmax"), node.getDoubleProperty("ymin"), node.getDoubleProperty("ymax"));
}

Continuous3DFunctionProxy::Continuous3DFunctionProxy() : SerializationProxy("Continuous3DFunction") {
}

void Continuous3DFunctionProxy::serialize(const void* object, SerializationNode& node) const {
    node.setIntProperty("version", 1);
    const Continuous3DFunction& function = *reinterpret_cast<const Continuous3DFunction*>(object);
    int xsize, ysize, zsize;
    double xmin, xmax, ymin, ymax, zmin, zmax;
    vector<double> values;
    function.getFunctionParameters(xsize, ysize, zsize, values, xmin, xmax, ymin, ymax, zmin, zmax);
    node.setDoubleProperty("xsize", xsize);
    node.setDoubleProperty("ysize", ysize);
    node.setDoubleProperty("zsize", zsize);
    node.setDoubleProperty("xmin", xmin);
    node.setDoubleProperty("xmax", xmax);
    node.setDoubleProperty("ymin", ymin);
    node.setDoubleProperty("ymax", ymax);
    node.setDoubleProperty("zmin", zmin);
    node.setDoubleProperty("zmax", zmax);
    SerializationNode& valuesNode = node.createChildNode("Values");
peastman's avatar
peastman committed
147
148
    for (auto v : values)
        valuesNode.createChildNode("Value").setDoubleProperty("v", v);
149
150
151
152
153
154
155
}

void* Continuous3DFunctionProxy::deserialize(const SerializationNode& node) const {
    if (node.getIntProperty("version") != 1)
        throw OpenMMException("Unsupported version number");
    const SerializationNode& valuesNode = node.getChildNode("Values");
    vector<double> values;
peastman's avatar
peastman committed
156
157
    for (auto& child : valuesNode.getChildren())
        values.push_back(child.getDoubleProperty("v"));
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    return new Continuous3DFunction(node.getIntProperty("xsize"), node.getIntProperty("ysize"), node.getIntProperty("zsize"), values,
            node.getDoubleProperty("xmin"), node.getDoubleProperty("xmax"), node.getDoubleProperty("ymin"), node.getDoubleProperty("ymax"),
            node.getDoubleProperty("zmin"), node.getDoubleProperty("zmax"));
}

Discrete1DFunctionProxy::Discrete1DFunctionProxy() : SerializationProxy("Discrete1DFunction") {
}

void Discrete1DFunctionProxy::serialize(const void* object, SerializationNode& node) const {
    node.setIntProperty("version", 1);
    const Discrete1DFunction& function = *reinterpret_cast<const Discrete1DFunction*>(object);
    vector<double> values;
    function.getFunctionParameters(values);
    SerializationNode& valuesNode = node.createChildNode("Values");
peastman's avatar
peastman committed
172
173
    for (auto v : values)
        valuesNode.createChildNode("Value").setDoubleProperty("v", v);
174
175
176
177
178
179
180
}

void* Discrete1DFunctionProxy::deserialize(const SerializationNode& node) const {
    if (node.getIntProperty("version") != 1)
        throw OpenMMException("Unsupported version number");
    const SerializationNode& valuesNode = node.getChildNode("Values");
    vector<double> values;
peastman's avatar
peastman committed
181
182
    for (auto& child : valuesNode.getChildren())
        values.push_back(child.getDoubleProperty("v"));
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    return new Discrete1DFunction(values);
}

Discrete2DFunctionProxy::Discrete2DFunctionProxy() : SerializationProxy("Discrete2DFunction") {
}

void Discrete2DFunctionProxy::serialize(const void* object, SerializationNode& node) const {
    node.setIntProperty("version", 1);
    const Discrete2DFunction& function = *reinterpret_cast<const Discrete2DFunction*>(object);
    int xsize, ysize;
    vector<double> values;
    function.getFunctionParameters(xsize, ysize, values);
    node.setDoubleProperty("xsize", xsize);
    node.setDoubleProperty("ysize", ysize);
    SerializationNode& valuesNode = node.createChildNode("Values");
peastman's avatar
peastman committed
198
199
    for (auto v : values)
        valuesNode.createChildNode("Value").setDoubleProperty("v", v);
200
201
202
203
204
205
206
}

void* Discrete2DFunctionProxy::deserialize(const SerializationNode& node) const {
    if (node.getIntProperty("version") != 1)
        throw OpenMMException("Unsupported version number");
    const SerializationNode& valuesNode = node.getChildNode("Values");
    vector<double> values;
peastman's avatar
peastman committed
207
208
    for (auto& child : valuesNode.getChildren())
        values.push_back(child.getDoubleProperty("v"));
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
    return new Discrete2DFunction(node.getIntProperty("xsize"), node.getIntProperty("ysize"), values);
}

Discrete3DFunctionProxy::Discrete3DFunctionProxy() : SerializationProxy("Discrete3DFunction") {
}

void Discrete3DFunctionProxy::serialize(const void* object, SerializationNode& node) const {
    node.setIntProperty("version", 1);
    const Discrete3DFunction& function = *reinterpret_cast<const Discrete3DFunction*>(object);
    int xsize, ysize, zsize;
    vector<double> values;
    function.getFunctionParameters(xsize, ysize, zsize, values);
    node.setDoubleProperty("xsize", xsize);
    node.setDoubleProperty("ysize", ysize);
    node.setDoubleProperty("zsize", zsize);
    SerializationNode& valuesNode = node.createChildNode("Values");
peastman's avatar
peastman committed
225
226
    for (auto v : values)
        valuesNode.createChildNode("Value").setDoubleProperty("v", v);
227
228
229
230
231
232
233
}

void* Discrete3DFunctionProxy::deserialize(const SerializationNode& node) const {
    if (node.getIntProperty("version") != 1)
        throw OpenMMException("Unsupported version number");
    const SerializationNode& valuesNode = node.getChildNode("Values");
    vector<double> values;
peastman's avatar
peastman committed
234
235
    for (auto& child : valuesNode.getChildren())
        values.push_back(child.getDoubleProperty("v"));
236
237
    return new Discrete3DFunction(node.getIntProperty("xsize"), node.getIntProperty("ysize"), node.getIntProperty("zsize"), values);
}