CustomIntegratorProxy.cpp 8.91 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-2024 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
33
34
35
36
37
38
39
40
41
 * Authors: Peter Eastman, Yutong Zhao                                        *
 * 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/CustomIntegratorProxy.h"
#include <OpenMM.h>

using namespace std;
using namespace OpenMM;

CustomIntegratorProxy::CustomIntegratorProxy() : SerializationProxy("CustomIntegrator") {
}

void CustomIntegratorProxy::serialize(const void* object, SerializationNode& node) const {
42
    node.setIntProperty("version", 3);
43
44
    const CustomIntegrator& integrator = *reinterpret_cast<const CustomIntegrator*>(object);
    SerializationNode& globalVariablesNode = node.createChildNode("GlobalVariables");
45
46
    for (int i = 0; i < integrator.getNumGlobalVariables(); i++)
        globalVariablesNode.createChildNode("Variable").setStringProperty("name", integrator.getGlobalVariableName(i)).setDoubleProperty("value", integrator.getGlobalVariable(i));
47
    SerializationNode& perDofVariablesNode = node.createChildNode("PerDofVariables");
48
    for (int i = 0; i < integrator.getNumPerDofVariables(); i++) {
49
50
        SerializationNode& perDofValuesNode = perDofVariablesNode.createChildNode(integrator.getPerDofVariableName(i));
        vector<Vec3> perDofValues; integrator.getPerDofVariable(i, perDofValues);
51
52
        for (int j = 0; j < perDofValues.size(); j++)
            perDofValuesNode.createChildNode("Value").setDoubleProperty("x", perDofValues[j][0]).setDoubleProperty("y", perDofValues[j][1]).setDoubleProperty("z", perDofValues[j][2]);
53
54
    }
    SerializationNode& computationsNode = node.createChildNode("Computations");
55
    for (int i = 0; i < integrator.getNumComputations(); i++) {
56
57
58
59
        CustomIntegrator::ComputationType computationType;
        string computationVariable;
        string computationExpression;
        integrator.getComputationStep(i, computationType, computationVariable, computationExpression);
60
61
        computationsNode.createChildNode("Computation").setIntProperty("computationType", static_cast<int>(computationType))
            .setStringProperty("computationVariable", computationVariable).setStringProperty("computationExpression", computationExpression);
62
    }
63
64
65
    SerializationNode& functions = node.createChildNode("Functions");
    for (int i = 0; i < integrator.getNumTabulatedFunctions(); i++)
        functions.createChildNode("Function", &integrator.getTabulatedFunction(i)).setStringProperty("name", integrator.getTabulatedFunctionName(i));
66
67
    node.setStringProperty("kineticEnergyExpression", integrator.getKineticEnergyExpression());
    node.setIntProperty("randomSeed", integrator.getRandomNumberSeed());
68
    node.setIntProperty("integrationForceGroups", integrator.getIntegrationForceGroups());
69
70
    node.setDoubleProperty("stepSize", integrator.getStepSize());
    node.setDoubleProperty("constraintTolerance", integrator.getConstraintTolerance());
71
72
73
}

void* CustomIntegratorProxy::deserialize(const SerializationNode& node) const {
74
    int version = node.getIntProperty("version");
75
    if (version < 1 || version > 3)
76
        throw OpenMMException("Unsupported version number");
77
    CustomIntegrator* integrator = new CustomIntegrator(node.getDoubleProperty("stepSize"));
78
    const SerializationNode& globalVariablesNode = node.getChildNode("GlobalVariables");
79
80
81
82
83
84
    if (version < 3)
        for (auto& prop : globalVariablesNode.getProperties())
            integrator->addGlobalVariable(prop.first, globalVariablesNode.getDoubleProperty(prop.first));
    else
        for (auto& var : globalVariablesNode.getChildren())
            integrator->addGlobalVariable(var.getStringProperty("name"), var.getDoubleProperty("value"));
85
86
    const SerializationNode& perDofVariablesNode = node.getChildNode("PerDofVariables");
    int count = 0;
peastman's avatar
peastman committed
87
88
    for (auto& var : perDofVariablesNode.getChildren()) {
        integrator->addPerDofVariable(var.getName(), 0);
89
        vector<Vec3> perDofValues;
peastman's avatar
peastman committed
90
91
        for (auto& child : var.getChildren())
            perDofValues.push_back(Vec3(child.getDoubleProperty("x"), child.getDoubleProperty("y"), child.getDoubleProperty("z")));
92
        integrator->setPerDofVariable(count, perDofValues);
peastman's avatar
peastman committed
93
        count++;
94
95
    }
    const SerializationNode& computationsNode = node.getChildNode("Computations");
peastman's avatar
peastman committed
96
97
    for (auto& comp : computationsNode.getChildren()) {
        CustomIntegrator::ComputationType computationType = static_cast<CustomIntegrator::ComputationType>(comp.getIntProperty("computationType"));
98
        // make sure that the int casts to a valid enum
peastman's avatar
peastman committed
99
100
101
102
103
104
105
        if (computationType == CustomIntegrator::ComputeGlobal) {
            integrator->addComputeGlobal(comp.getStringProperty("computationVariable"), comp.getStringProperty("computationExpression"));
        } else if (computationType == CustomIntegrator::ComputePerDof) {
            integrator->addComputePerDof(comp.getStringProperty("computationVariable"), comp.getStringProperty("computationExpression"));
        } else if (computationType == CustomIntegrator::ComputeSum) {
            integrator->addComputeSum(comp.getStringProperty("computationVariable"), comp.getStringProperty("computationExpression"));
        } else if (computationType == CustomIntegrator::ConstrainPositions) {
106
            integrator->addConstrainPositions();
peastman's avatar
peastman committed
107
        } else if (computationType == CustomIntegrator::ConstrainVelocities) {
108
            integrator->addConstrainVelocities();
peastman's avatar
peastman committed
109
        } else if (computationType == CustomIntegrator::UpdateContextState) {
110
            integrator->addUpdateContextState();
peastman's avatar
peastman committed
111
112
113
114
115
116
        } else if (computationType == CustomIntegrator::IfBlockStart) {
            integrator->beginIfBlock(comp.getStringProperty("computationExpression"));
        } else if (computationType == CustomIntegrator::WhileBlockStart) {
            integrator->beginWhileBlock(comp.getStringProperty("computationExpression"));
        } else if (computationType == CustomIntegrator::BlockEnd) {
            integrator->endBlock();
117
118
119
120
        } else {
            throw(OpenMMException("Custom Integrator Deserialization: Unknown computation type"));
        }
    }
121
122
123
124
125
    if (version > 1) {
        const SerializationNode& functions = node.getChildNode("Functions");
        for (auto& function : functions.getChildren())
            integrator->addTabulatedFunction(function.getStringProperty("name"), function.decodeObject<TabulatedFunction>());
    }
126
127
    integrator->setKineticEnergyExpression(node.getStringProperty("kineticEnergyExpression"));
    integrator->setRandomNumberSeed(node.getIntProperty("randomSeed"));
128
    integrator->setIntegrationForceGroups(node.getIntProperty("integrationForceGroups", 0xFFFFFFFF));
129
130
    integrator->setConstraintTolerance(node.getDoubleProperty("constraintTolerance"));
    return integrator;
131
}