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-2021.
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.potential.parameters.ForceField.ForceFieldType.STRBND;
41  import static ffx.utilities.KeywordGroup.EnergyUnitConversion;
42  import static ffx.utilities.KeywordGroup.PotentialFunctionParameter;
43  import static java.lang.Double.parseDouble;
44  import static java.lang.Integer.parseInt;
45  import static java.util.Arrays.copyOf;
46  import static org.apache.commons.math3.util.FastMath.PI;
47  
48  import ffx.utilities.FFXKeyword;
49  import java.util.Arrays;
50  import java.util.Comparator;
51  import java.util.HashMap;
52  import java.util.logging.Level;
53  import java.util.logging.Logger;
54  
55  /**
56   * The StretchBendType class defines one out-of-plane angle bending energy type.
57   *
58   * @author Michael J. Schnieders
59   * @since 1.0
60   */
61  @FFXKeyword(name = "strbnd", clazz = String.class, keywordGroup = PotentialFunctionParameter,
62      description = "[3 integers and 2 reals] "
63          + "Provides the values for a single stretch-bend cross term potential parameter. "
64          + "The integer modifiers give the atom class numbers for the three kinds of atoms involved in the angle which is to be defined. "
65          + "The real number modifiers give the force constant values for the first bond (first two atom classes) with the angle, and the second bond with the angle, respectively. "
66          + "The default units for the stretch-bend force constant are kcal/mole/Ang-radian, but this can be controlled via the strbndunit keyword.")
67  public final class StretchBendType extends BaseType implements Comparator<String> {
68  
69    /** Constant <code>units=PI / 180.0</code> */
70    public static final double DEFAULT_STRBND_UNIT = PI / 180.0;
71  
72    @FFXKeyword(name = "strbndunit", keywordGroup = EnergyUnitConversion, defaultValue = "(Pi/180)",
73        description =
74            "Sets the scale factor needed to convert the energy value computed by the bond stretching-angle bending cross term potential into units of kcal/mole. "
75                + "The correct value is force field dependent and typically provided in the header of the master force field parameter file.")
76    public double strbndunit = DEFAULT_STRBND_UNIT;
77  
78    private static final Logger logger = Logger.getLogger(StretchBendType.class.getName());
79    /** Atom class for this stretch-bend type. */
80    public final int[] atomClasses;
81    /** Force constants (Kcal/mole/Angstrom-Degrees). */
82    public final double[] forceConstants;
83  
84    /**
85     * StretchBendType Constructor.
86     *
87     * @param atomClasses int[]
88     * @param forceConstants double[]
89     */
90    public StretchBendType(int[] atomClasses, double[] forceConstants) {
91      super(STRBND, sortKey(copyOf(atomClasses, 3)));
92      // Sort the atom classes and force constants in tandem.
93      if (atomClasses[0] > atomClasses[2]) {
94        int temp = atomClasses[0];
95        double f = forceConstants[0];
96        atomClasses[0] = atomClasses[2];
97        forceConstants[0] = forceConstants[1];
98        atomClasses[2] = temp;
99        forceConstants[1] = f;
100     }
101     this.atomClasses = atomClasses;
102     this.forceConstants = forceConstants;
103   }
104 
105   /**
106    * average.
107    *
108    * @param stretchBendType1 a {@link ffx.potential.parameters.StretchBendType} object.
109    * @param stretchBendType2 a {@link ffx.potential.parameters.StretchBendType} object.
110    * @param atomClasses an array of {@link int} objects.
111    * @return a {@link ffx.potential.parameters.StretchBendType} object.
112    */
113   public static StretchBendType average(
114       StretchBendType stretchBendType1, StretchBendType stretchBendType2, int[] atomClasses) {
115     if (stretchBendType1 == null || stretchBendType2 == null || atomClasses == null) {
116       return null;
117     }
118     int len = stretchBendType1.forceConstants.length;
119     if (len != stretchBendType2.forceConstants.length) {
120       return null;
121     }
122     double[] forceConstants = new double[len];
123     for (int i = 0; i < len; i++) {
124       forceConstants[i] =
125           (stretchBendType1.forceConstants[i] + stretchBendType2.forceConstants[i]) / 2.0;
126     }
127     return new StretchBendType(atomClasses, forceConstants);
128   }
129 
130   /**
131    * Construct a StretchBendType from an input string.
132    *
133    * @param input The overall input String.
134    * @param tokens The input String tokenized.
135    * @return a StretchBendType instance.
136    */
137   public static StretchBendType parse(String input, String[] tokens) {
138     if (tokens.length < 6) {
139       logger.log(Level.WARNING, "Invalid STRBND type:\n{0}", input);
140     } else {
141       try {
142         int[] atomClasses = new int[3];
143         atomClasses[0] = parseInt(tokens[1]);
144         atomClasses[1] = parseInt(tokens[2]);
145         atomClasses[2] = parseInt(tokens[3]);
146         double[] forceConstants = new double[2];
147         forceConstants[0] = parseDouble(tokens[4]);
148         forceConstants[1] = parseDouble(tokens[5]);
149         return new StretchBendType(atomClasses, forceConstants);
150       } catch (NumberFormatException e) {
151         String message = "Exception parsing STRBND type:\n" + input + "\n";
152         logger.log(Level.SEVERE, message, e);
153       }
154     }
155     return null;
156   }
157 
158   /**
159    * This method sorts the atom classes as: min, c[1], max
160    *
161    * @param c atomClasses
162    * @return lookup key
163    */
164   public static String sortKey(int[] c) {
165     if (c == null || c.length != 3) {
166       return null;
167     }
168     if (c[0] > c[2]) {
169       int temp = c[0];
170       c[0] = c[2];
171       c[2] = temp;
172     }
173     return c[0] + " " + c[1] + " " + c[2];
174   }
175 
176   /** {@inheritDoc} */
177   @Override
178   public int compare(String key1, String key2) {
179     String[] keys1 = key1.split(" ");
180     String[] keys2 = key2.split(" ");
181     int[] c1 = new int[3];
182     int[] c2 = new int[3];
183     for (int i = 0; i < 3; i++) {
184       c1[i] = parseInt(keys1[i]);
185       c2[i] = parseInt(keys2[i]);
186     }
187     if (c1[1] < c2[1]) {
188       return -1;
189     } else if (c1[1] > c2[1]) {
190       return 1;
191     } else if (c1[0] < c2[0]) {
192       return -1;
193     } else if (c1[0] > c2[0]) {
194       return 1;
195     } else if (c1[2] < c2[2]) {
196       return -1;
197     } else if (c1[2] > c2[2]) {
198       return 1;
199     }
200     return 0;
201   }
202 
203   /** {@inheritDoc} */
204   @Override
205   public boolean equals(Object o) {
206     if (this == o) {
207       return true;
208     }
209     if (o == null || getClass() != o.getClass()) {
210       return false;
211     }
212     StretchBendType stretchBendType = (StretchBendType) o;
213     return Arrays.equals(atomClasses, stretchBendType.atomClasses);
214   }
215 
216   /** {@inheritDoc} */
217   @Override
218   public int hashCode() {
219     return Arrays.hashCode(atomClasses);
220   }
221 
222   /**
223    * incrementClasses
224    *
225    * @param increment a int.
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   /**
235    * Remap new atom classes to known internal ones.
236    *
237    * @param typeMap a lookup between new atom types and known atom types.
238    * @return a {@link ffx.potential.parameters.StretchBendType} object.
239    */
240   public StretchBendType patchClasses(HashMap<AtomType, AtomType> typeMap) {
241     int count = 0;
242     int len = atomClasses.length;
243 
244     // Check if this StetchBendType contain 1 or 2 mapped atom classes.
245     for (AtomType newType : typeMap.keySet()) {
246       for (int atomClass : atomClasses) {
247         if (atomClass == newType.atomClass) {
248           count++;
249         }
250       }
251     }
252 
253     // If found, create a new StetchBendType that bridges to known classes.
254     if (count == 1 || count == 2) {
255       int[] newClasses = Arrays.copyOf(atomClasses, len);
256       for (AtomType newType : typeMap.keySet()) {
257         for (int i = 0; i < len; i++) {
258           if (atomClasses[i] == newType.atomClass) {
259             AtomType knownType = typeMap.get(newType);
260             newClasses[i] = knownType.atomClass;
261           }
262         }
263       }
264 
265       return new StretchBendType(newClasses, forceConstants);
266     }
267     return null;
268   }
269 
270   /**
271    * {@inheritDoc}
272    *
273    * <p>Nicely formatted Stretch-Bend string.
274    */
275   @Override
276   public String toString() {
277     return String.format(
278         "strbnd  %5d  %5d  %5d  %6.2f  %6.2f",
279         atomClasses[0], atomClasses[1], atomClasses[2], forceConstants[0], forceConstants[1]);
280   }
281 }