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.fft;
39  
40  import static org.junit.Assert.assertEquals;
41  import static org.junit.Assert.assertTrue;
42  
43  import java.util.Arrays;
44  import java.util.Collection;
45  import java.util.Random;
46  
47  import ffx.utilities.FFXTest;
48  import org.junit.Test;
49  import org.junit.runner.RunWith;
50  import org.junit.runners.Parameterized;
51  import org.junit.runners.Parameterized.Parameters;
52  
53  /** @author Michael J. Schnieders */
54  @RunWith(Parameterized.class)
55  public class ComplexTest extends FFXTest {
56  
57    private final int n;
58    private final String info;
59    private final boolean preferred;
60    private final double[] data;
61    private final double[] orig;
62    private final double[] dft;
63  
64    public ComplexTest(String info, int n, boolean preferred) {
65      this.info = info;
66      this.n = n;
67      this.preferred = preferred;
68      data = new double[n * 2];
69      orig = new double[n * 2];
70      dft = new double[n * 2];
71      Random r = new Random();
72      for (int i = 0; i < n; i++) {
73        double d = r.nextDouble();
74        data[i * 2] = d;
75        orig[i * 2] = d;
76      }
77    }
78  
79    @Parameters
80    public static Collection<Object[]> data() {
81      return Arrays.asList(
82          new Object[][] {
83              {"Test n = 162 with factors [6, 3, 3, 3]", 162, true},
84              {"Test n = 160 with factors [5, 4, 4, 2]", 160, true},
85              {"Test n = 120 with factors [6, 5, 4]", 120, true},
86              {"Test n = 64 with factors [4, 4, 4]", 64, true},
87              {"Test n = 48 with factors [6, 4, 2]", 48, true},
88              {"Test n = 21 with factors [7, 3]", 21, true},
89              {"Test n = 38 with factors [2, 19]", 38, false},
90              {"Test n = 22 with factors [2, 11]", 22, false},
91          });
92    }
93  
94    /** Test of fft method, of class Complex. */
95    @Test
96    public void testFft() {
97      double tolerance = 1.0e-11;
98  
99      int offset = 0;
100     int stride = 2;
101     Complex complex = new Complex(n);
102 
103     // System.out.println(info + "\n Factors " + Arrays.toString(complex.getFactors()));
104 
105     long dftTime = System.nanoTime();
106     Complex.dft(data, dft);
107     dftTime = System.nanoTime() - dftTime;
108     String dftString = " DFT Time: " + dftTime * 1.0e-9 + " s\n";
109 
110     long fftTime = System.nanoTime();
111     complex.fft(data, offset, stride);
112     fftTime = System.nanoTime() - fftTime;
113     String fftString = " FFT Time: " + fftTime * 1.0e-9 + " s";
114 
115     // Test the FFT is equals the DFT result.
116     for (int i = 0; i < 2 * n; i++) {
117       assertEquals(" Forward " + info + " at position: " + i, dft[i], data[i], tolerance);
118     }
119 
120     // The FFT is faster than the DFT.
121     String message = fftString + dftString;
122     // assertTrue(message, fftTime < dftTime);
123 
124     // Test that X = IFFT(FFT(X)).
125     complex.inverse(data, offset, stride);
126     for (int i = 0; i < n; i++) {
127       double orig = this.orig[i * 2];
128       double actual = data[i * 2];
129       assertEquals(" IFFT(FFT(X)) " + info + " at position: " + i, orig, actual, tolerance);
130     }
131   }
132 
133   /** Test of preferredDimension method, of class Complex. */
134   @Test
135   public void testPreferredDimension() {
136     boolean result = Complex.preferredDimension(n);
137     assertEquals(info, preferred, result);
138   }
139 }