"serialization/vscode:/vscode.git/clone" did not exist on "d314e695a83252ff6cbfe07a4b25f446caae33a6"
TestSerializeAmoebaTorsionForce.cpp 5.12 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
/* -------------------------------------------------------------------------- *
 *                                OpenMMAmoeba                                *
 * -------------------------------------------------------------------------- *
 * 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 "../../../tests/AssertionUtilities.h"
#include "openmm/AmoebaTorsionForce.h"
#include "openmm/serialization/XmlSerializer.h"
#include <iostream>
#include <sstream>

using namespace OpenMM;
using namespace std;

void loadTorsion( std::vector<double>& torsion, double offset ){
    torsion.push_back( offset + 1.0 );
    torsion.push_back( offset + 2.0 );
//    torsion.push_back( offset + 3.0 );
//    torsion.push_back( offset + 4.0 );
}

void compareTorsion( std::vector<double> torsionA, std::vector<double> torsionB ){
    ASSERT_EQUAL(torsionA.size(), torsionB.size());
50
51
    for (unsigned int ii = 0; ii < torsionA.size(); ii++) {
        ASSERT_EQUAL(torsionA[ii], torsionB[ii]);
52
53
54
55
    }
}

void testSerialization() {
56

57
58
    // Create a Force.

59
60
    AmoebaTorsionForce force1;
    for( unsigned int ii = 0; ii < 5; ii++ ){
61
62
63
64
65
66
        std::vector<double> torsion1;
        std::vector<double> torsion2;
        std::vector<double> torsion3;
        loadTorsion( torsion1, static_cast<double>(5*ii) + 11.1);
        loadTorsion( torsion2, static_cast<double>(5*ii) + 21.2);
        loadTorsion( torsion3, static_cast<double>(5*ii) + 31.3);
67
        force1.addTorsion( ii, ii+1,ii+3, ii+4, torsion1, torsion2, torsion3 );
68
69
70
71
72
    }

    // Serialize and then deserialize it.

    stringstream buffer;
73
    XmlSerializer::serialize<AmoebaTorsionForce>(&force1, "Force", buffer);
74

Mark Friedrichs's avatar
Mark Friedrichs committed
75
#ifdef AMOEBA_DEBUG
76
77
78
79
80
    if( 0 ){
        FILE* filePtr = fopen("Torsion.xml", "w" );
        (void) fprintf( filePtr, "%s", buffer.str().c_str() );
        (void) fclose( filePtr );
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
81
#endif
82
83
84
85
86

    AmoebaTorsionForce* copy = XmlSerializer::deserialize<AmoebaTorsionForce>(buffer);

    // Compare the two forces to see if they are identical.  
    AmoebaTorsionForce& force2 = *copy;
87
88
    ASSERT_EQUAL(force1.getNumTorsions(), force2.getNumTorsions());
    for (unsigned int ii = 0; ii < force1.getNumTorsions(); ii++) {
89
90
91
92
93
94
95
96
97
98

        int a1, a2, a3, a4, b1, b2, b3, b4;

        std::vector<double> torsion1a;
        std::vector<double> torsion2a;
        std::vector<double> torsion3a;
        std::vector<double> torsion1b;
        std::vector<double> torsion2b;
        std::vector<double> torsion3b;

99
100
        force1.getTorsionParameters( ii, a1, a2, a3, a4, torsion1a, torsion2a, torsion3a);
        force2.getTorsionParameters( ii, b1, b2, b3, b4, torsion1b, torsion2b, torsion3b);
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

        ASSERT_EQUAL(a1, b1);
        ASSERT_EQUAL(a2, b2);
        ASSERT_EQUAL(a3, b3);
        ASSERT_EQUAL(a4, b4);

        compareTorsion( torsion1a, torsion1b );
        compareTorsion( torsion2a, torsion2b );
        compareTorsion( torsion3a, torsion3b );
    }
}

int main() {
    try {
        testSerialization();
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
    return 0;
}