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.PITORS;
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.lang.String.format;
46  
47  import ffx.utilities.FFXKeyword;
48  import java.util.Arrays;
49  import java.util.Comparator;
50  import java.util.HashMap;
51  import java.util.logging.Level;
52  import java.util.logging.Logger;
53  
54  /**
55   * The PiOrbitalTorsionType class defines a Pi-Orbital Torsion energy term.
56   *
57   * @author Michael J. Schnieders
58   * @since 1.0
59   */
60  @FFXKeyword(name = "pitors", clazz = String.class, keywordGroup = PotentialFunctionParameter,
61      description = "[2 integers and 1 real] "
62          + "Provides the values for a single pi-orbital torsional angle potential parameter. "
63          + "The two integer modifiers give the atom class numbers for the atoms involved in the central bond of the torsional angle to be parameterized. "
64          + "The real modifier gives the value of the 2-fold Fourier amplitude for the torsional angle between p-orbitals centered on the defined bond atom classes. "
65          + "The default units for the stretch-torsion force constant can be controlled via the pitorsunit keyword.")
66  public final class PiOrbitalTorsionType extends BaseType implements Comparator<String> {
67  
68    /** A Logger for the PiTorsionType class. */
69    private static final Logger logger = Logger.getLogger(PiOrbitalTorsionType.class.getName());
70  
71    public static final double DEFAULT_PITORS_UNIT = 1.0;
72  
73    /** Convert Pi-Torsion energy to kcal/mole. */
74    @FFXKeyword(name = "pitorsunit", keywordGroup = EnergyUnitConversion, defaultValue = "1.0",
75        description = "Sets the scale factor needed to convert the energy value computed by the pi-orbital torsional angle potential into units of kcal/mole. "
76            + "The correct value is force field dependent and typically provided in the header of the master force field parameter file.")
77    public double piTorsUnit = DEFAULT_PITORS_UNIT;
78  
79    /** Atom classes that form this Pi-Torsion. */
80    public final int[] atomClasses;
81  
82    /** Force constant. */
83    public double forceConstant;
84  
85    /**
86     * PiTorsionType Constructor.
87     *
88     * @param atomClasses int[]
89     * @param forceConstant double
90     */
91    public PiOrbitalTorsionType(int[] atomClasses, double forceConstant) {
92      super(PITORS, sortKey(atomClasses));
93      this.atomClasses = atomClasses;
94      this.forceConstant = forceConstant;
95    }
96  
97    /**
98     * Average two PiTorsionType instances. The atom classes that define the new type must be
99     * supplied.
100    *
101    * @param piOrbitalTorsionType1 a {@link PiOrbitalTorsionType} object.
102    * @param piOrbitalTorsionType2 a {@link PiOrbitalTorsionType} object.
103    * @param atomClasses an array of {@link int} objects.
104    * @return a {@link PiOrbitalTorsionType} object.
105    */
106   public static PiOrbitalTorsionType average(
107       PiOrbitalTorsionType piOrbitalTorsionType1, PiOrbitalTorsionType piOrbitalTorsionType2,
108       int[] atomClasses) {
109     if (piOrbitalTorsionType1 == null || piOrbitalTorsionType2 == null || atomClasses == null) {
110       return null;
111     }
112 
113     double forceConstant =
114         (piOrbitalTorsionType1.forceConstant + piOrbitalTorsionType2.forceConstant) / 2.0;
115 
116     return new PiOrbitalTorsionType(atomClasses, forceConstant);
117   }
118 
119   /**
120    * Construct a PiTorsionType from an input string.
121    *
122    * @param input The overall input String.
123    * @param tokens The input String tokenized.
124    * @return a PiTorsionType instance.
125    */
126   public static PiOrbitalTorsionType parse(String input, String[] tokens) {
127     if (tokens.length < 4) {
128       logger.log(Level.WARNING, "Invalid PITORS type:\n{0}", input);
129     } else {
130       try {
131         int[] atomClasses = new int[2];
132         atomClasses[0] = parseInt(tokens[1]);
133         atomClasses[1] = parseInt(tokens[2]);
134         double forceConstant = parseDouble(tokens[3]);
135         return new PiOrbitalTorsionType(atomClasses, forceConstant);
136       } catch (NumberFormatException e) {
137         String message = "Exception parsing PITORS type:\n" + input + "\n";
138         logger.log(Level.SEVERE, message, e);
139       }
140     }
141     return null;
142   }
143 
144   /**
145    * This method sorts the atom classes as: min, max
146    *
147    * @param c atomClasses
148    * @return lookup key
149    */
150   public static String sortKey(int[] c) {
151     if (c == null || c.length != 2) {
152       return null;
153     }
154 
155     int temp;
156     if (c[1] <= c[0]) {
157       temp = c[1];
158       c[1] = c[0];
159       c[0] = temp;
160     }
161     return c[0] + " " + c[1];
162   }
163 
164   /** {@inheritDoc} */
165   @Override
166   public int compare(String s1, String s2) {
167     String[] keys1 = s1.split(" ");
168     String[] keys2 = s2.split(" ");
169 
170     for (int i = 0; i < 2; i++) {
171       int c1 = parseInt(keys1[i]);
172       int c2 = parseInt(keys2[i]);
173       if (c1 < c2) {
174         return -1;
175       } else if (c1 > c2) {
176         return 1;
177       }
178     }
179     return 0;
180   }
181 
182   /** {@inheritDoc} */
183   @Override
184   public boolean equals(Object o) {
185     if (this == o) {
186       return true;
187     }
188     if (o == null || getClass() != o.getClass()) {
189       return false;
190     }
191     PiOrbitalTorsionType piOrbitalTorsionType = (PiOrbitalTorsionType) o;
192     return Arrays.equals(atomClasses, piOrbitalTorsionType.atomClasses);
193   }
194 
195   /** {@inheritDoc} */
196   @Override
197   public int hashCode() {
198     return Arrays.hashCode(atomClasses);
199   }
200 
201   /**
202    * incrementClasses
203    *
204    * @param increment a int.
205    */
206   public void incrementClasses(int increment) {
207     for (int i = 0; i < atomClasses.length; i++) {
208       atomClasses[i] += increment;
209     }
210     setKey(sortKey(atomClasses));
211   }
212 
213   /**
214    * Remap new atom classes to known internal ones.
215    *
216    * @param typeMap a lookup between new atom types and known atom types.
217    * @return a {@link PiOrbitalTorsionType} object.
218    */
219   public PiOrbitalTorsionType patchClasses(HashMap<AtomType, AtomType> typeMap) {
220     int count = 0;
221     int len = atomClasses.length;
222 
223     // Look for new PiTorsions that contain 1 mapped atom classes.
224     for (AtomType newType : typeMap.keySet()) {
225 
226       for (int atomClass : atomClasses) {
227         if (atomClass == newType.atomClass) {
228           count++;
229         }
230       }
231     }
232 
233     // If found, create a new PiTorsion that bridges to known classes.
234     if (count == 1) {
235       int[] newClasses = Arrays.copyOf(atomClasses, len);
236       for (AtomType newType : typeMap.keySet()) {
237         for (int i = 0; i < len; i++) {
238           if (atomClasses[i] == newType.atomClass) {
239             AtomType knownType = typeMap.get(newType);
240             newClasses[i] = knownType.atomClass;
241           }
242         }
243       }
244       return new PiOrbitalTorsionType(newClasses, forceConstant);
245     }
246     return null;
247   }
248 
249   /**
250    * {@inheritDoc}
251    *
252    * <p>Nicely formatted Pi-Torsion type.
253    */
254   @Override
255   public String toString() {
256     return format("pitors  %5d  %5d  %4.2f", atomClasses[0], atomClasses[1], forceConstant);
257   }
258 
259   /**
260    * setScaleFactor.
261    *
262    * @param scale a double.
263    */
264   void setScaleFactor(double scale) {
265     forceConstant *= scale;
266   }
267 }