StateProxy.cpp 10 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
52
53
54
55
56
57
/* -------------------------------------------------------------------------- *
 *                                   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) 2010 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/StateProxy.h"
#include <OpenMM.h>
#include <map>

using namespace std;
using namespace OpenMM;

StateProxy::StateProxy() : SerializationProxy("State") {

}

void StateProxy::serialize(const void* object, SerializationNode& node) const {
    node.setIntProperty("version", 1);
    const State& s = *reinterpret_cast<const State*>(object);
    node.setDoubleProperty("time", s.getTime());
    Vec3 a,b,c;
    s.getPeriodicBoxVectors(a,b,c);
    SerializationNode& boxVectorsNode = node.createChildNode("PeriodicBoxVectors");
    boxVectorsNode.createChildNode("A").setDoubleProperty("x", a[0]).setDoubleProperty("y", a[1]).setDoubleProperty("z", a[2]);
    boxVectorsNode.createChildNode("B").setDoubleProperty("x", b[0]).setDoubleProperty("y", b[1]).setDoubleProperty("z", b[2]);
    boxVectorsNode.createChildNode("C").setDoubleProperty("x", c[0]).setDoubleProperty("y", c[1]).setDoubleProperty("z", c[2]);
    try {
        s.getParameters();
        SerializationNode& parametersNode = node.createChildNode("Parameters");
        map<string, double> stateParams = s.getParameters();
        map<string, double>::const_iterator it;
58
        for (it = stateParams.begin(); it!=stateParams.end();it++) {
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
            parametersNode.setDoubleProperty(it->first, it->second);
        }
    } catch (const OpenMMException &) {
        // do nothing
    }
    try {
        s.getPotentialEnergy();
        SerializationNode& energiesNode = node.createChildNode("Energies");
        energiesNode.setDoubleProperty("PotentialEnergy", s.getPotentialEnergy());
        energiesNode.setDoubleProperty("KineticEnergy", s.getKineticEnergy());
    } catch (const OpenMMException &) {
        // do nothing
    }
    try {
        s.getPositions();
        SerializationNode& positionsNode = node.createChildNode("Positions");
        vector<Vec3> statePositions = s.getPositions();
76
        for (int i=0; i<statePositions.size();i++) {
77
           positionsNode.createChildNode("Position").setDoubleProperty("x", statePositions[i][0]).setDoubleProperty("y", statePositions[i][1]).setDoubleProperty("z", statePositions[i][2]);
78
79
80
81
82
83
84
85
        }
    } catch (const OpenMMException &) {
        // do nothing
    }
    try {
        s.getVelocities();
        SerializationNode& velocitiesNode = node.createChildNode("Velocities");
        vector<Vec3> stateVelocities = s.getVelocities();
86
        for (int i=0; i<stateVelocities.size();i++) {
87
           velocitiesNode.createChildNode("Velocity").setDoubleProperty("x", stateVelocities[i][0]).setDoubleProperty("y", stateVelocities[i][1]).setDoubleProperty("z", stateVelocities[i][2]);
88
89
90
91
92
93
94
95
        }
    } catch (const OpenMMException &) {
        // do nothing
    }
    try {
        s.getForces();
        SerializationNode& forcesNode = node.createChildNode("Forces");
        vector<Vec3> stateForces = s.getForces();
96
        for (int i=0; i<stateForces.size();i++) {
97
            forcesNode.createChildNode("Force").setDoubleProperty("x", stateForces[i][0]).setDoubleProperty("y", stateForces[i][1]).setDoubleProperty("z", stateForces[i][2]);
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
        }
    } catch (const OpenMMException &) {
        // do nothing
    }
}

void* StateProxy::deserialize(const SerializationNode& node) const {
    if (node.getIntProperty("version") != 1 && node.getIntProperty("version") != 2)
        throw OpenMMException("Unsupported version number");
    double outTime = node.getDoubleProperty("time");
    const SerializationNode& boxVectorsNode = node.getChildNode("PeriodicBoxVectors");
    const SerializationNode& AVec = boxVectorsNode.getChildNode("A");
    Vec3 outAVec(AVec.getDoubleProperty("x"),AVec.getDoubleProperty("y"),AVec.getDoubleProperty("z"));
    const SerializationNode& BVec = boxVectorsNode.getChildNode("B");
    Vec3 outBVec(BVec.getDoubleProperty("x"),BVec.getDoubleProperty("y"),BVec.getDoubleProperty("z"));
    const SerializationNode& CVec = boxVectorsNode.getChildNode("C");
    Vec3 outCVec(CVec.getDoubleProperty("x"),CVec.getDoubleProperty("y"),CVec.getDoubleProperty("z"));
    int types = 0;
    map<string, double> outStateParams;
    try {
        const SerializationNode& parametersNode = node.getChildNode("Parameters");
        // inStateParams is really a <string,double> pair, where string is the name and double is the value
        // but we want to avoid casting a string to a double and instead use the built in routines,
        map<string, string> inStateParams = parametersNode.getProperties();
122
        for (map<string, string>::const_iterator pit = inStateParams.begin(); pit != inStateParams.end(); pit++) {
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
            outStateParams[pit->first] = parametersNode.getDoubleProperty(pit->first);
        }
        types = types | State::Parameters;
    } catch (const OpenMMException &) {
        // do nothing
    }
    double potentialEnergy;
    double kineticEnergy;
    try {
        const SerializationNode& energiesNode = node.getChildNode("Energies");
        potentialEnergy = energiesNode.getDoubleProperty("PotentialEnergy");
        kineticEnergy = energiesNode.getDoubleProperty("KineticEnergy");
        types = types | State::Energy;
    } catch (const OpenMMException &) {
        // do nothing    
    }
    vector<Vec3> outPositions;
    vector<Vec3> outVelocities;
    vector<Vec3> outForces;
    try {
        const SerializationNode& positionsNode = node.getChildNode("Positions");
144
        for (int i = 0; i < (int) positionsNode.getChildren().size(); i++) {
145
146
147
148
149
150
151
152
153
            const SerializationNode& particle = positionsNode.getChildren()[i];
            outPositions.push_back(Vec3(particle.getDoubleProperty("x"),particle.getDoubleProperty("y"),particle.getDoubleProperty("z")));
        }
        types = types | State::Positions;
    } catch (const OpenMMException &) {
        // do nothing    
    }
    try {
        const SerializationNode& velocitiesNode = node.getChildNode("Velocities");
154
        for (int i = 0; i < (int) velocitiesNode.getChildren().size(); i++) {
155
156
157
158
159
160
161
162
163
            const SerializationNode& particle = velocitiesNode.getChildren()[i];
            outVelocities.push_back(Vec3(particle.getDoubleProperty("x"),particle.getDoubleProperty("y"),particle.getDoubleProperty("z")));
        }
        types = types | State::Velocities;
    } catch (const OpenMMException &) {
        // do nothing    
    }
    try {
        const SerializationNode& forcesNode = node.getChildNode("Forces");
164
        for (int i = 0; i < (int) forcesNode.getChildren().size(); i++) {
165
166
167
168
169
170
            const SerializationNode& particle = forcesNode.getChildren()[i];
            outForces.push_back(Vec3(particle.getDoubleProperty("x"),particle.getDoubleProperty("y"),particle.getDoubleProperty("z")));
        }
        types = types | State::Forces;
    } catch (const OpenMMException &) {
        // do nothing
171
    }
172
    vector<int> arraySizes;
173
174
175
    State::StateBuilder builder(outTime);
    if (types & State::Positions) {
        builder.setPositions(outPositions);
176
177
        arraySizes.push_back(outPositions.size());
    }  
178
179
    if (types & State::Velocities) {
        builder.setVelocities(outVelocities);
180
181
        arraySizes.push_back(outVelocities.size());
    }
182
183
184
    if (types & State::Forces) {
        builder.setForces(outForces);
        arraySizes.push_back(outForces.size());
185
    }
186
187
    if (types & State::Energy) {
        builder.setEnergy(kineticEnergy, potentialEnergy);
188
    }
189
190
    if (types & State::Parameters) {
        builder.setParameters(outStateParams);
191
192
    }

193
194
    for (int i = 1; i < arraySizes.size(); i++) {
        if (arraySizes[i] != arraySizes[i-1]) {
195
196
197
            throw(OpenMMException("State Deserialization Particle Size Mismatch, check number of particles in Forces, Velocities, Positions!"));
        }
    }
198
199
200
    builder.setPeriodicBoxVectors(outAVec, outBVec, outCVec);
    State *s = new State();
    *s = builder.getState();
201
    return s;
202
}