TabulatedFunctionProxies.cpp 10.6 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
    bool periodic;
55
    periodic = function.getPeriodic();
56
    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
}

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
86
87
    for (auto v : values)
        valuesNode.createChildNode("Value").setDoubleProperty("v", v);
88
89
90
91
92
93
94
}

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

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

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

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
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
198
    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
199
200
    for (auto v : values)
        valuesNode.createChildNode("Value").setDoubleProperty("v", v);
201
202
203
204
205
206
207
}

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