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.potential.parameters;
39  
40  import static ffx.utilities.PropertyGroup.PotentialFunctionParameter;
41  import static java.lang.Double.parseDouble;
42  import static java.lang.Integer.parseInt;
43  import static java.lang.String.format;
44  
45  import ffx.potential.parameters.ForceField.ForceFieldType;
46  import ffx.utilities.FFXProperty;
47  
48  import java.util.Arrays;
49  import java.util.Comparator;
50  import java.util.logging.Level;
51  import java.util.logging.Logger;
52  
53  /**
54   * The VDWPairType class defines a van der Waals Pair type.
55   *
56   * @author Michael J. Schnieders
57   * @since 1.0
58   */
59  @FFXProperty(name = "vdwpr", clazz = String.class, propertyGroup = PotentialFunctionParameter, description = """
60      [2 integers and 2 reals]
61      Provides the values for the vdw parameters for a single special heteroatomic pair of atoms.
62      The integer modifiers give the pair of atom class numbers for which special vdw parameters are to be defined.
63      The two real number modifiers give the values of the minimum energy contact distance in Angstroms and the well depth at the minimum distance in kcal/mole.
64      """)
65  public final class VDWPairType extends BaseType implements Comparator<String> {
66  
67    private static final Logger logger = Logger.getLogger(VDWPairType.class.getName());
68    /**
69     * The radius of the minimum well depth energy (angstroms).
70     */
71    public final double radius;
72    /**
73     * The minimum energy of the vdw function (kcal/mol).
74     */
75    public final double wellDepth;
76    /**
77     * Atom classes that form this bond stretch.
78     */
79    public final int[] atomClasses;
80  
81    /**
82     * van der Waals constructor. If the reduction factor is .LE. 0.0, no reduction is used for this
83     * atom type.
84     *
85     * @param atomClasses The atom class that uses this van der Waals Pair.
86     * @param radius      The radius of the minimum well depth energy (angstroms).
87     * @param wellDepth   The minimum energy of the vdw function (kcal/mol).
88     */
89    public VDWPairType(int[] atomClasses, double radius, double wellDepth) {
90      super(ForceFieldType.VDWPR, sortKey(atomClasses));
91      this.atomClasses = atomClasses;
92      this.radius = radius;
93      this.wellDepth = wellDepth;
94    }
95  
96    /**
97     * Average.
98     *
99     * @param vdwType1    a {@link VDWPairType} object.
100    * @param vdwType2    a {@link VDWPairType} object.
101    * @param atomClasses The atom classes that uses this van der Waals Pair.
102    * @return a {@link VDWPairType} object.
103    */
104   public static VDWPairType average(VDWPairType vdwType1, VDWPairType vdwType2, int[] atomClasses) {
105     if (vdwType1 == null || vdwType2 == null) {
106       return null;
107     }
108 
109     double radius = (vdwType1.radius + vdwType2.radius) / 2.0;
110     double wellDepth = (vdwType1.wellDepth + vdwType2.wellDepth) / 2.0;
111 
112     return new VDWPairType(atomClasses, radius, wellDepth);
113   }
114 
115   /**
116    * Construct a VDWPairType from multiple input lines.
117    *
118    * @param input  The overall input String.
119    * @param tokens The input String tokenized.
120    * @return a VDWType instance.
121    */
122   public static VDWPairType parse(String input, String[] tokens) {
123     if (tokens.length < 5) {
124       logger.log(Level.WARNING, "Invalid VDWPR type:\n{0}", input);
125     } else {
126       try {
127         int atomClass1 = parseInt(tokens[1]);
128         int atomClass2 = parseInt(tokens[2]);
129         double radius = parseDouble(tokens[3]);
130         double wellDepth = parseDouble(tokens[4]);
131         return new VDWPairType(new int[]{atomClass1, atomClass2}, radius, wellDepth);
132       } catch (NumberFormatException e) {
133         String message = "Exception parsing VDWPR type:\n" + input + "\n";
134         logger.log(Level.SEVERE, message, e);
135       }
136     }
137     return null;
138   }
139 
140   /**
141    * {@inheritDoc}
142    */
143   @Override
144   public int compare(String key1, String key2) {
145     String[] keys1 = key1.split(" ");
146     String[] keys2 = key2.split(" ");
147     int[] c1 = new int[2];
148     int[] c2 = new int[2];
149     for (int i = 0; i < 2; i++) {
150       c1[i] = Integer.parseInt(keys1[i]);
151       c2[i] = Integer.parseInt(keys2[i]);
152     }
153 
154     if (c1[0] < c2[0]) {
155       return -1;
156     } else if (c1[0] > c2[0]) {
157       return 1;
158     } else if (c1[1] < c2[1]) {
159       return -1;
160     } else if (c1[1] > c2[1]) {
161       return 1;
162     }
163 
164     return 0;
165   }
166 
167   /**
168    * {@inheritDoc}
169    */
170   @Override
171   public boolean equals(Object o) {
172     if (this == o) {
173       return true;
174     }
175     if (o == null || getClass() != o.getClass()) {
176       return false;
177     }
178     VDWPairType vdwPairType = (VDWPairType) o;
179     return Arrays.equals(atomClasses, vdwPairType.atomClasses);
180   }
181 
182   /**
183    * {@inheritDoc}
184    */
185   @Override
186   public int hashCode() {
187     return Arrays.hashCode(atomClasses);
188   }
189 
190   /**
191    * {@inheritDoc}
192    *
193    * <p>Nicely formatted van der Waals type.
194    */
195   @Override
196   public String toString() {
197     return format("vdwpr  %5d  %5d  %11.9f  %11.9f", atomClasses[0], atomClasses[1], radius,
198         wellDepth);
199   }
200 
201   /**
202    * This method sorts the atom classes as: min, max
203    *
204    * @param c atomClasses
205    * @return lookup key
206    */
207   public static String sortKey(int[] c) {
208     if (c == null || c.length != 2) {
209       return null;
210     }
211 
212     int temp;
213     if (c[1] <= c[0]) {
214       temp = c[1];
215       c[1] = c[0];
216       c[0] = temp;
217     }
218 
219     return c[0] + " " + c[1];
220   }
221 
222   /**
223    * Increment the atom classes by a specified amount.
224    *
225    * @param increment The increment to add to the atom classes.
226    */
227   public void incrementClasses(int increment) {
228     for (int i = 0; i < atomClasses.length; i++) {
229       atomClasses[i] += increment;
230     }
231     setKey(sortKey(atomClasses));
232   }
233 
234 }