StateProxy.cpp 8.41 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-2015 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
 * 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"
33
34
35
#include "openmm/Platform.h"
#include "openmm/State.h"
#include "openmm/Vec3.h"
36
37
38
39
40
41
42
43
44
45
46
#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);
47
    node.setStringProperty("openmmVersion", Platform::getOpenMMVersion());
48
49
50
51
52
53
54
55
    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]);
56
    if ((s.getDataTypes()&State::Parameters) != 0) {
57
58
        s.getParameters();
        SerializationNode& parametersNode = node.createChildNode("Parameters");
peastman's avatar
peastman committed
59
60
        for (auto& param : s.getParameters())
            parametersNode.setDoubleProperty(param.first, param.second);
61
    }
62
    if ((s.getDataTypes()&State::Energy) != 0) {
63
64
65
66
67
        s.getPotentialEnergy();
        SerializationNode& energiesNode = node.createChildNode("Energies");
        energiesNode.setDoubleProperty("PotentialEnergy", s.getPotentialEnergy());
        energiesNode.setDoubleProperty("KineticEnergy", s.getKineticEnergy());
    }
68
    if ((s.getDataTypes()&State::Positions) != 0) {
69
70
71
        s.getPositions();
        SerializationNode& positionsNode = node.createChildNode("Positions");
        vector<Vec3> statePositions = s.getPositions();
72
        for (int i=0; i<statePositions.size();i++) {
73
           positionsNode.createChildNode("Position").setDoubleProperty("x", statePositions[i][0]).setDoubleProperty("y", statePositions[i][1]).setDoubleProperty("z", statePositions[i][2]);
74
75
        }
    }
76
    if ((s.getDataTypes()&State::Velocities) != 0) {
77
78
79
        s.getVelocities();
        SerializationNode& velocitiesNode = node.createChildNode("Velocities");
        vector<Vec3> stateVelocities = s.getVelocities();
80
        for (int i=0; i<stateVelocities.size();i++) {
81
           velocitiesNode.createChildNode("Velocity").setDoubleProperty("x", stateVelocities[i][0]).setDoubleProperty("y", stateVelocities[i][1]).setDoubleProperty("z", stateVelocities[i][2]);
82
83
        }
    }
84
    if ((s.getDataTypes()&State::Forces) != 0) {
85
86
87
        s.getForces();
        SerializationNode& forcesNode = node.createChildNode("Forces");
        vector<Vec3> stateForces = s.getForces();
88
        for (int i=0; i<stateForces.size();i++) {
89
            forcesNode.createChildNode("Force").setDoubleProperty("x", stateForces[i][0]).setDoubleProperty("y", stateForces[i][1]).setDoubleProperty("z", stateForces[i][2]);
90
91
92
93
94
        }
    }
}

void* StateProxy::deserialize(const SerializationNode& node) const {
95
    if (node.getIntProperty("version") != 1)
96
97
98
99
100
101
102
103
104
105
        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;
106
107
    vector<int> arraySizes;
    State::StateBuilder builder(outTime);
peastman's avatar
peastman committed
108
    for (auto& child : node.getChildren()) {
109
110
        if (child.getName() == "Parameters") {
            map<string, double> outStateParams;
peastman's avatar
peastman committed
111
112
            for (auto& param : child.getProperties())
                outStateParams[param.first] = child.getDoubleProperty(param.first);
113
            builder.setParameters(outStateParams);
114
        }
115
116
117
118
        else if (child.getName() == "Energies") {
            double potentialEnergy = child.getDoubleProperty("PotentialEnergy");
            double kineticEnergy = child.getDoubleProperty("KineticEnergy");
            builder.setEnergy(kineticEnergy, potentialEnergy);
119
        }
120
121
        else if (child.getName() == "Positions") {
            vector<Vec3> outPositions;
peastman's avatar
peastman committed
122
            for (auto& particle : child.getChildren())
123
124
125
                outPositions.push_back(Vec3(particle.getDoubleProperty("x"),particle.getDoubleProperty("y"),particle.getDoubleProperty("z")));
            builder.setPositions(outPositions);
            arraySizes.push_back(outPositions.size());
126
        }
127
128
        else if (child.getName() == "Velocities") {
            vector<Vec3> outVelocities;
peastman's avatar
peastman committed
129
            for (auto& particle : child.getChildren())
130
131
132
133
134
135
                outVelocities.push_back(Vec3(particle.getDoubleProperty("x"),particle.getDoubleProperty("y"),particle.getDoubleProperty("z")));
            builder.setVelocities(outVelocities);
            arraySizes.push_back(outVelocities.size());
        }
        else if (child.getName() == "Forces") {
            vector<Vec3> outForces;
peastman's avatar
peastman committed
136
            for (auto& particle : child.getChildren())
137
138
139
                outForces.push_back(Vec3(particle.getDoubleProperty("x"),particle.getDoubleProperty("y"),particle.getDoubleProperty("z")));
            builder.setForces(outForces);
            arraySizes.push_back(outForces.size());
140
141
        }
    }
142
143
    for (int i = 1; i < arraySizes.size(); i++) {
        if (arraySizes[i] != arraySizes[i-1]) {
144
145
146
            throw(OpenMMException("State Deserialization Particle Size Mismatch, check number of particles in Forces, Velocities, Positions!"));
        }
    }
147
148
149
    builder.setPeriodicBoxVectors(outAVec, outBVec, outCVec);
    State *s = new State();
    *s = builder.getState();
150
    return s;
151
}