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.crystal;
39  
40  import static org.apache.commons.math3.util.FastMath.floor;
41  import static org.apache.commons.math3.util.FastMath.max;
42  import static org.apache.commons.math3.util.FastMath.min;
43  
44  /**
45   * The ReflectionSpline class represents a reflection spline basis.
46   *
47   * @author Timothy D. Fenn
48   * @see <a href="http://dx.doi.org/10.1107/S0021889802013420" target="_blank"> Cowtan, K. 2002.
49   * Generic representation and evaluation of properties as a function of position in reciprocal
50   * space. J. Appl. Cryst. 35:655-663. </a>
51   * @since 1.0
52   */
53  public class ReflectionSpline {
54  
55    private final ReflectionList reflectionList;
56    private final int nParams;
57    private double f;
58    private int i0, i1, i2;
59    private double dfi0, dfi1, dfi2;
60  
61    /**
62     * Constructor for ReflectionSpline.
63     *
64     * @param reflectionList a {@link ffx.crystal.ReflectionList} object.
65     * @param nParams        an int.
66     */
67    public ReflectionSpline(ReflectionList reflectionList, int nParams) {
68      this.reflectionList = reflectionList;
69      this.nParams = nParams;
70    }
71  
72    /**
73     * dfi0
74     *
75     * @return a double.
76     */
77    public double dfi0() {
78      return dfi0;
79    }
80  
81    /**
82     * dfi1
83     *
84     * @return a double.
85     */
86    public double dfi1() {
87      return dfi1;
88    }
89  
90    /**
91     * dfi2
92     *
93     * @return a double.
94     */
95    public double dfi2() {
96      return dfi2;
97    }
98  
99    /**
100    * f
101    *
102    * @return a double.
103    */
104   public double f() {
105     return f;
106   }
107 
108   /**
109    * Evaluate basis function and derivative at a given resolution
110    * <p>
111    * Equations 24 and 25 in Cowtan et al.
112    *
113    * @param invResSq resolution of desired spline interpolation
114    * @param params   current spline parameters
115    * @return value at invResSq
116    */
117   public double f(double invResSq, double[] params) {
118     double s = nParams * reflectionList.ordinal(invResSq);
119     int i = (int) floor(s);
120     double ds = s - i - 0.5;
121     i0 = min(max(0, i - 1), nParams - 1);
122     i1 = min(max(0, i), nParams - 1);
123     i2 = min(max(0, i + 1), nParams - 1);
124 
125     f =
126         params[i0] * 0.5 * (ds - 0.5) * (ds - 0.5)
127             + params[i1] * (0.75 - ds * ds)
128             + params[i2] * 0.5 * (ds + 0.5) * (ds + 0.5);
129 
130     dfi0 = 0.5 * (ds - 0.5) * (ds - 0.5);
131     dfi1 = 0.75 - ds * ds;
132     dfi2 = 0.5 * (ds + 0.5) * (ds + 0.5);
133 
134     return f;
135   }
136 
137   /**
138    * i0
139    *
140    * @return an int.
141    */
142   public int i0() {
143     return i0;
144   }
145 
146   /**
147    * i1
148    *
149    * @return an int.
150    */
151   public int i1() {
152     return i1;
153   }
154 
155   /**
156    * i2
157    *
158    * @return an int.
159    */
160   public int i2() {
161     return i2;
162   }
163 }