TabulatedFunctionProxies.cpp 10.7 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
/* -------------------------------------------------------------------------- *
 *                                   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 {
44
    node.setIntProperty("version", 2);
45
46
47
48
49
50
51
    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
    bool periodic;
55
    periodic = function.getPeriodic();
56
    node.setBoolProperty("periodic", periodic);
57
58
59
}

void* Continuous1DFunctionProxy::deserialize(const SerializationNode& node) const {
60
61
    int version = node.getIntProperty("version");
    if (version < 1 || version > 2)
62
63
64
        throw OpenMMException("Unsupported version number");
    const SerializationNode& valuesNode = node.getChildNode("Values");
    vector<double> values;
peastman's avatar
peastman committed
65
66
    for (auto& child : valuesNode.getChildren())
        values.push_back(child.getDoubleProperty("v"));
67
68
69
70
71
72
    bool periodic;
    if (version == 1)
        periodic = false;
    else
        periodic = node.getBoolProperty("periodic");
    return new Continuous1DFunction(values, node.getDoubleProperty("min"), node.getDoubleProperty("max"), periodic);
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
}

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
92
93
    for (auto v : values)
        valuesNode.createChildNode("Value").setDoubleProperty("v", v);
94
95
96
97
98
99
100
}

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
101
102
    for (auto& child : valuesNode.getChildren())
        values.push_back(child.getDoubleProperty("v"));
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    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
127
128
    for (auto v : values)
        valuesNode.createChildNode("Value").setDoubleProperty("v", v);
129
130
131
132
133
134
135
}

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
136
137
    for (auto& child : valuesNode.getChildren())
        values.push_back(child.getDoubleProperty("v"));
138
139
140
141
142
143
144
145
146
147
148
149
150
151
    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
152
153
    for (auto v : values)
        valuesNode.createChildNode("Value").setDoubleProperty("v", v);
154
155
156
157
158
159
160
}

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
161
162
    for (auto& child : valuesNode.getChildren())
        values.push_back(child.getDoubleProperty("v"));
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
    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
178
179
    for (auto v : values)
        valuesNode.createChildNode("Value").setDoubleProperty("v", v);
180
181
182
183
184
185
186
}

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
187
188
    for (auto& child : valuesNode.getChildren())
        values.push_back(child.getDoubleProperty("v"));
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
    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
205
206
    for (auto v : values)
        valuesNode.createChildNode("Value").setDoubleProperty("v", v);
207
208
209
210
211
212
213
}

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
214
215
    for (auto& child : valuesNode.getChildren())
        values.push_back(child.getDoubleProperty("v"));
216
217
    return new Discrete3DFunction(node.getIntProperty("xsize"), node.getIntProperty("ysize"), node.getIntProperty("zsize"), values);
}