TestSplineFitter.cpp 8.24 KB
Newer Older
Peter Eastman's avatar
Peter Eastman committed
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
/* -------------------------------------------------------------------------- *
 *                                   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.                                     *
 * -------------------------------------------------------------------------- */

#ifdef WIN32
  #define _USE_MATH_DEFINES // Needed to get M_PI
#endif
35
#include "openmm/internal/AssertionUtilities.h"
Peter Eastman's avatar
Peter Eastman committed
36
37
38
39
40
41
42
43
44
45
46
#include "openmm/internal/SplineFitter.h"
#include <cmath>
#include <iostream>
#include <vector>

using namespace OpenMM;
using namespace std;

void testNaturalSpline() {
    vector<double> x(20);
    vector<double> y(20);
47
48
49
    for (unsigned int i = 0; i < x.size(); i++) {
        x[i] = 0.5*i+0.1*sin(double(i));
        y[i] = sin(double(x[i]));
Peter Eastman's avatar
Peter Eastman committed
50
51
52
53
54
    }
    vector<double> deriv;
    SplineFitter::createNaturalSpline(x, y, deriv);
    ASSERT_EQUAL_TOL(deriv[0], 0.0, 1e-6);
    ASSERT_EQUAL_TOL(deriv[deriv.size()-1], 0.0, 1e-6);
55
    for (unsigned int i = 0; i < x.size(); i++) {
Peter Eastman's avatar
Peter Eastman committed
56
57
58
59
        ASSERT_EQUAL_TOL(y[i], SplineFitter::evaluateSpline(x, y, deriv, x[i]), 1e-6);
        ASSERT_EQUAL_TOL(cos(x[i]), SplineFitter::evaluateSplineDerivative(x, y, deriv, x[i]), 0.05);
    }
    for (int i = 1; i < 9; i++) {
60
61
        ASSERT_EQUAL_TOL(sin((double)i), SplineFitter::evaluateSpline(x, y, deriv, i), 0.05);
        ASSERT_EQUAL_TOL(cos((double)i), SplineFitter::evaluateSplineDerivative(x, y, deriv, i), 0.05);
Peter Eastman's avatar
Peter Eastman committed
62
63
64
65
66
67
    }
}

void testPeriodicSpline() {
    vector<double> x(26);
    vector<double> y(26);
68
69
70
    for (unsigned int i = 0; i < x.size()-1; i++) {
        x[i] = 0.5*i+0.1*sin((double)i);
        y[i] = sin((double)x[i]);
Peter Eastman's avatar
Peter Eastman committed
71
72
73
74
75
76
    }
    x[x.size()-1] = 4*M_PI;
    y[y.size()-1] = y[0];
    vector<double> deriv;
    SplineFitter::createPeriodicSpline(x, y, deriv);
    ASSERT_EQUAL_TOL(deriv[0], deriv[deriv.size()-1], 1e-6);
77
    for (unsigned int i = 0; i < x.size(); i++) {
Peter Eastman's avatar
Peter Eastman committed
78
79
80
81
        ASSERT_EQUAL_TOL(y[i], SplineFitter::evaluateSpline(x, y, deriv, x[i]), 1e-6);
        ASSERT_EQUAL_TOL(cos(x[i]), SplineFitter::evaluateSplineDerivative(x, y, deriv, x[i]), 0.05);
    }
    for (int i = 1; i < 9; i++) {
82
83
        ASSERT_EQUAL_TOL(sin((double)i), SplineFitter::evaluateSpline(x, y, deriv, i), 0.05);
        ASSERT_EQUAL_TOL(cos((double)i), SplineFitter::evaluateSplineDerivative(x, y, deriv, i), 0.05);
Peter Eastman's avatar
Peter Eastman committed
84
    }
85
86
87
88
89
90
    double ya[] = {15.579, 16.235, 17.325, 18.741, 20.454, 22.517, 24.944, 27.554, 29.942, 31.657,
                   32.486, 32.612, 32.494, 32.532, 32.785, 32.917, 32.402, 30.842, 28.229, 24.989,
                   21.762, 19.074, 17.147, 15.970, 15.467, 15.579};
    y.assign(ya, ya+y.size());
    ASSERT_EQUAL_TOL(SplineFitter::evaluateSplineDerivative(x, y, deriv, x[0]),
                     SplineFitter::evaluateSplineDerivative(x, y, deriv, x[x.size()-1]), 1e-6);
Peter Eastman's avatar
Peter Eastman committed
91
92
}

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
void test2DSpline() {
    const int xsize = 15;
    const int ysize = 17;
    vector<double> x(xsize);
    vector<double> y(ysize);
    vector<double> f(xsize*ysize);
    for (int i = 0; i < xsize; i++)
        x[i] = 0.5*i+0.1*sin(double(i));
    for (int i = 0; i < ysize; i++)
        y[i] = 0.6*i+0.1*sin(double(i));
    for (int i = 0; i < xsize; i++)
        for (int j = 0; j < ysize; j++)
            f[i+j*xsize] = sin(x[i])*cos(0.4*y[j]);
    vector<vector<double> > c;
    SplineFitter::create2DNaturalSpline(x, y, f, c);
    for (int i = 0; i < xsize; i++)
        for (int j = 0; j < ysize; j++) {
            double value = SplineFitter::evaluate2DSpline(x, y, f, c, x[i], y[j]);
            ASSERT_EQUAL_TOL(f[i+j*xsize], value, 1e-6);
        }
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
115
116
            double s = x[0]+(i+1)*(x[xsize-1]-x[0])/12.0;
            double t = y[0]+(j+1)*(y[ysize-1]-y[0])/12.0;
117
            double value = SplineFitter::evaluate2DSpline(x, y, f, c, s, t);
118
            ASSERT_EQUAL_TOL(sin(s)*cos(0.4*t), value, 0.02);
119
120
            double dx, dy;
            SplineFitter::evaluate2DSplineDerivatives(x, y, f, c, s, t, dx, dy);
121
122
            ASSERT_EQUAL_TOL(cos(s)*cos(0.4*t), dx, 0.05);
            ASSERT_EQUAL_TOL(-0.4*sin(s)*sin(0.4*t), dy, 0.05);
123
124
125
126
        }
    }
}

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
void test3DSpline() {
    const int xsize = 8;
    const int ysize = 9;
    const int zsize = 10;
    vector<double> x(xsize);
    vector<double> y(ysize);
    vector<double> z(zsize);
    vector<double> f(xsize*ysize*zsize);
    for (int i = 0; i < xsize; i++)
        x[i] = 0.2*i+0.02*sin(0.4*double(i));
    for (int i = 0; i < ysize; i++)
        y[i] = 0.2*i+0.02*sin(0.45*double(i));
    for (int i = 0; i < zsize; i++)
        z[i] = 0.2*i+0.02*sin(0.5*double(i));
    for (int i = 0; i < xsize; i++)
        for (int j = 0; j < ysize; j++)
            for (int k = 0; k < zsize; k++)
                f[i+j*xsize+k*xsize*ysize] = sin(x[i])*cos(0.4*y[j])*(1+z[k]);
    vector<vector<double> > c;
    SplineFitter::create3DNaturalSpline(x, y, z, f, c);
    for (int i = 0; i < xsize; i++)
        for (int j = 0; j < ysize; j++) {
            for (int k = 0; k < zsize; k++) {
                double value = SplineFitter::evaluate3DSpline(x, y, z, f, c, x[i], y[j], z[k]);
                ASSERT_EQUAL_TOL(f[i+j*xsize+k*xsize*ysize], value, 1e-6);
            }
        }
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            for (int k = 0; k < 10; k++) {
                double s = x[0]+(i+1)*(x[xsize-1]-x[0])/12.0;
                double t = y[0]+(j+1)*(y[ysize-1]-y[0])/12.0;
                double u = z[0]+(k+1)*(z[zsize-1]-z[0])/12.0;
                double value = SplineFitter::evaluate3DSpline(x, y, z, f, c, s, t, u);
                ASSERT_EQUAL_TOL(sin(s)*cos(0.4*t)*(1+u), value, 0.02);
                double dx, dy, dz;
                SplineFitter::evaluate3DSplineDerivatives(x, y, z, f, c, s, t, u, dx, dy, dz);
                ASSERT_EQUAL_TOL(cos(s)*cos(0.4*t)*(1+u), dx, 0.1);
                ASSERT_EQUAL_TOL(-0.4*sin(s)*sin(0.4*t)*(1+u), dy, 0.1);
                ASSERT_EQUAL_TOL(sin(s)*cos(0.4*t), dz, 0.1);
            }
        }
    }
}

Peter Eastman's avatar
Peter Eastman committed
172
173
174
175
int main() {
    try {
        testNaturalSpline();
        testPeriodicSpline();
176
        test2DSpline();
177
        test3DSpline();
Peter Eastman's avatar
Peter Eastman committed
178
179
180
181
182
183
184
185
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
    return 0;
}