View Javadoc
1   // ******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2024.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  // ******************************************************************************
38  package ffx.numerics.spline;
39  
40  import static org.junit.Assert.assertEquals;
41  
42  import java.util.Arrays;
43  import java.util.Collection;
44  
45  import ffx.utilities.FFXTest;
46  import org.junit.Test;
47  import org.junit.runner.RunWith;
48  import org.junit.runners.Parameterized;
49  import org.junit.runners.Parameterized.Parameters;
50  
51  /**
52   * Parameterized test of the UniformBSpline class.
53   *
54   * @author Michael J. Schnieders
55   * @since 1.0
56   */
57  @RunWith(Parameterized.class)
58  public class UniformBSplineTest extends FFXTest {
59  
60    private static double tolerance = 1.0e-6;
61    private final String info;
62    private final int order;
63    private final double x;
64    private final double[] coefficients;
65    private final double[] expected;
66    private final double[] expectedDiff;
67    private final int deriveOrder;
68    private final double[][] theta;
69    private final double[][] bSplineWork;
70  
71    /**
72     * Parameterized test of the UniformBSpline class.
73     *
74     * @param info
75     * @param order
76     * @param x
77     * @param expected
78     * @param expectedDiff
79     * @since 1.0
80     */
81    public UniformBSplineTest(
82        String info, int order, double x, double[] expected, double[] expectedDiff) {
83      this.info = info;
84      this.order = order;
85      this.x = x;
86      this.expected = expected;
87      this.expectedDiff = expectedDiff;
88  
89      deriveOrder = order - 1;
90      coefficients = new double[order];
91      theta = new double[order][deriveOrder + 1];
92      bSplineWork = new double[order][order];
93    }
94  
95    @Parameters
96    public static Collection<Object[]> data() {
97      // Order 3, x = 0.0 (
98      double[] c30 = {0.500000e0, 0.5000000e0, 0.000000e0};
99      double[] d30 = {-1.0000000e0, 1.0000000e0, 0.0000000e0};
100     // Order 3, x = 0.5 (At a grid point).
101     double[] c31 = {0.1250000e0, 0.7500000e0, 0.1250000e0};
102     double[] d31 = {-0.5000000e0, 0.0000000e0, 0.5000000e0};
103 
104     // Order 5, x = 0.00
105     double[] c50 = {0.0416666e0, 0.4583333e0, 0.4583333e0, 0.0416666e0, 0.0000000e0};
106     double[] d50 = {-0.1666667e0, -0.5000000e0, 0.5000000e0, 0.1666667e0, 0.0000000e0};
107     // Order 5, x = 0.25
108     double[] c51 = {0.0131836e0, 0.3248700e0, 0.5608720e0, 0.1009110e0, 0.0001627e0};
109     double[] d51 = {-0.0703125e0, -0.5416670e0, 0.2968750e0, 0.3125000e0, 0.0026042e0};
110     // Order 5, x = 0.50
111     double[] c52 = {0.0026041e0, 0.1979167e0, 0.5989583e0, 0.1979167e0, 0.0026041e0};
112     double[] d52 = {-0.0208333e0, -0.4583333e0, 0.0000000e0, 0.4583333e0, 0.0208333e0};
113     // Order 5, x = 0.75
114     double[] c53 = {0.0001627e0, 0.1009110e0, 0.5608720e0, 0.3248700e0, 0.0131836e0};
115     double[] d53 = {-0.0026042e0, -0.3125000e0, -0.2968750e0, 0.5416670e0, 0.0703125e0};
116     // Order 5, x = 1.0
117     double[] c54 = {0.0000000e0, 0.0416666e0, 0.4583333e0, 0.4583333e0, 0.0416666e0};
118     double[] d54 = {0.0000000e0, -0.1666667e0, -0.5000000e0, 0.5000000e0, 0.1666667e0};
119 
120     return Arrays.asList(
121         new Object[][] {
122             {"Order 3, x = 0.00", 3, 0.00e0, c30, d30},
123             {"Order 3, x = 0.50", 3, 0.50e0, c31, d31},
124             {"Order 5, x = 0.00", 5, 0.00e0, c50, d50},
125             {"Order 5, x = 0.25", 5, 0.25e0, c51, d51},
126             {"Order 5, x = 0.50", 5, 0.50e0, c52, d52},
127             {"Order 5, x = 0.75", 5, 0.75e0, c53, d53},
128             {"Order 5, x = 1.00", 5, 1.00e0, c54, d54}
129         });
130   }
131 
132   /**
133    * Test of bSpline method, of class UniformBSpline.
134    *
135    * @since 1.0
136    */
137   @Test
138   public void testBSpline() {
139     UniformBSpline.bSpline(x, order, coefficients);
140     for (int i = 0; i < order; i++) {
141       assertEquals(info, expected[i], coefficients[i], tolerance);
142     }
143   }
144 
145   /**
146    * Test of bSplineDerivatives method, of class UniformBSpline.
147    *
148    * @since 1.0
149    */
150   @Test
151   public void testBSplineDerivatives() {
152     UniformBSpline.bSplineDerivatives(x, order, deriveOrder, theta, bSplineWork);
153     for (int i = 0; i < order; i++) {
154       double actual = theta[i][0];
155       assertEquals(info, expected[i], actual, tolerance);
156       actual = theta[i][1];
157       assertEquals(info, expectedDiff[i], actual, tolerance);
158     }
159   }
160 }