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-2025.
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.terms;
39  
40  import ffx.potential.bonded.Angle;
41  import ffx.potential.bonded.BondedTerm;
42  import ffx.potential.parameters.AngleType;
43  
44  import java.util.ArrayList;
45  import java.util.Collection;
46  import java.util.Collections;
47  import java.util.List;
48  import java.util.logging.Logger;
49  
50  import static java.lang.String.format;
51  import static org.apache.commons.math3.util.FastMath.PI;
52  
53  /**
54   * Angle potential energy term using {@link ffx.potential.bonded.Angle} instances.
55   *
56   * @author Michael J. Schnieders
57   * @since 1.0
58   */
59  public class AnglePotentialEnergy extends EnergyTerm {
60  
61    private static final Logger logger = Logger.getLogger(AnglePotentialEnergy.class.getName());
62  
63    /**
64     * Internal list of Angle instances.
65     */
66    private final List<Angle> angles = new ArrayList<>();
67  
68    /**
69     * Create an AnglePotentialEnergy with the provided name.
70     *
71     * @param name Name for this term.
72     */
73    public AnglePotentialEnergy(String name) {
74      super(name);
75    }
76  
77    /**
78     * Create an AnglePotentialEnergy with the provided name and force group.
79     *
80     * @param name       Name for this term.
81     * @param forceGroup Integer force group identifier.
82     */
83    public AnglePotentialEnergy(String name, int forceGroup) {
84      super(name, forceGroup);
85    }
86  
87    /**
88     * Create an AnglePotentialEnergy initialized with a list of angles and force group.
89     *
90     * @param name       Name for this term.
91     * @param forceGroup Force group identifier.
92     * @param angles     List of Angle instances to add (null-safe).
93     */
94    public AnglePotentialEnergy(String name, int forceGroup, List<Angle> angles) {
95      super(name, forceGroup);
96      if (angles != null) {
97        Collections.sort(angles);
98        this.angles.addAll(angles);
99        logger.info(format("  Angles:                            %10d", getNumberOfAngles()));
100     }
101   }
102 
103   /**
104    * Get the number of terms in this potential energy term.
105    *
106    * @return The number of terms, which is the same as the number of angles.
107    */
108   @Override
109   public int getNumberOfTerms() {
110     return getNumberOfAngles();
111   }
112 
113   /**
114    * Get an array of BondedTerms in this term.
115    *
116    * @return Array of BondedTerms, which are actually Angles in this case.
117    */
118   @Override
119   public BondedTerm[] getBondedTermsArray() {
120     return getAngleArray();
121   }
122 
123   /**
124    * Create an AnglePotentialEnergy initialized with a collection of angles.
125    *
126    * @param name   Name for this term (may be null).
127    * @param angles Collection of Angle instances to add (null-safe).
128    */
129   public AnglePotentialEnergy(String name, Collection<Angle> angles) {
130     super(name);
131     if (angles != null) {
132       this.angles.addAll(angles);
133     }
134   }
135 
136   /**
137    * Add an Angle to this term.
138    *
139    * @param angle Angle to add (ignored if null).
140    * @return true if the angle was added.
141    */
142   public boolean addAngle(Angle angle) {
143     if (angle == null) {
144       return false;
145     }
146     return angles.add(angle);
147   }
148 
149   /**
150    * Add an array of Angles to this term.
151    *
152    * @param angles Array of Angle instances to add.
153    * @return true if the angles were added.
154    */
155   public boolean addAngles(Angle[] angles) {
156     if (angles == null) {
157       return false;
158     }
159     Collections.addAll(this.angles, angles);
160     return true;
161   }
162 
163   /**
164    * Add a list of Angles to this term.
165    *
166    * @param angles List of Angle instances to add.
167    * @return true if the angles were added.
168    */
169   public boolean addAngles(List<Angle> angles) {
170     if (angles == null) {
171       return false;
172     }
173     this.angles.addAll(angles);
174     return true;
175   }
176 
177   /**
178    * Remove an Angle from this term.
179    *
180    * @param angle Angle to remove (ignored if null).
181    * @return true if the angle was present and removed.
182    */
183   public boolean removeAngle(Angle angle) {
184     if (angle == null) {
185       return false;
186     }
187     return angles.remove(angle);
188   }
189 
190   /**
191    * Get the Angle at a given index.
192    *
193    * @param index Index in the internal list.
194    * @return Angle at the specified index.
195    * @throws IndexOutOfBoundsException if index is invalid.
196    */
197   public Angle getAngle(int index) {
198     return angles.get(index);
199   }
200 
201   /**
202    * Get an unmodifiable view of the Angles in this term.
203    *
204    * @return Unmodifiable List of Angles.
205    */
206   public List<Angle> getAngles() {
207     return Collections.unmodifiableList(angles);
208   }
209 
210   /**
211    * Get an array of Angles in this term.
212    *
213    * @return Array of Angles.
214    */
215   public Angle[] getAngleArray() {
216     return angles.toArray(new Angle[0]);
217   }
218 
219   /**
220    * Get the number of Angles in this term.
221    *
222    * @return The number of Angles.
223    */
224   public int getNumberOfAngles() {
225     return angles.size();
226   }
227 
228   /**
229    * Get the String used for OpenMM angle energy expressions.
230    * @return String representing the angle energy expression.
231    */
232   public String getAngleEnergyString() {
233     AngleType angleType = angles.getFirst().angleType;
234     String energy;
235     if (angleType.angleFunction == AngleType.AngleFunction.SEXTIC) {
236       energy = format("""
237               k*(d^2 + %.15g*d^3 + %.15g*d^4 + %.15g*d^5 + %.15g*d^6);
238               d=%.15g*theta-theta0;
239               """,
240           angleType.cubic, angleType.quartic, angleType.pentic, angleType.sextic, 180.0 / PI);
241     } else {
242       energy = format("""
243               k*(d^2);
244               d=%.15g*theta-theta0;
245               """,
246           180.0 / PI);
247     }
248     return energy;
249   }
250 
251   public String getInPlaneAngleEnergyString() {
252     AngleType angleType = angles.getFirst().angleType;
253     String energy = format("""
254             k*(d^2 + %.15g*d^3 + %.15g*d^4 + %.15g*d^5 + %.15g*d^6);
255             d=theta-theta0;
256             theta = %.15g*pointangle(x1, y1, z1, projx, projy, projz, x3, y3, z3);
257             projx = x2-nx*dot;
258             projy = y2-ny*dot;
259             projz = z2-nz*dot;
260             dot = nx*(x2-x3) + ny*(y2-y3) + nz*(z2-z3);
261             nx = px/norm;
262             ny = py/norm;
263             nz = pz/norm;
264             norm = sqrt(px*px + py*py + pz*pz);
265             px = (d1y*d2z-d1z*d2y);
266             py = (d1z*d2x-d1x*d2z);
267             pz = (d1x*d2y-d1y*d2x);
268             d1x = x1-x4;
269             d1y = y1-y4;
270             d1z = z1-z4;
271             d2x = x3-x4;
272             d2y = y3-y4;
273             d2z = z3-z4;
274             """,
275         angleType.cubic, angleType.quartic, angleType.pentic, angleType.sextic, 180.0 / PI);
276     return energy;
277   }
278 
279   /**
280    * Log the details of Angle interactions.
281    */
282   @Override
283   public void log() {
284     if (getNumberOfAngles() <= 0) {
285       return;
286     }
287     logger.info("\n Angle Bending Interactions:");
288     for (Angle angle : getAngles()) {
289       logger.info(" Angle \t" + angle.toString());
290     }
291   }
292 
293   @Override
294   public String toPDBString() {
295     if (getNumberOfAngles() <= 0) {
296       return "";
297     }
298     StringBuilder sb = new StringBuilder();
299     sb.append(format("REMARK   3   %s %g (%d)\n", "ANGLE BENDING              : ", getEnergy(), getNumberOfAngles()));
300     sb.append(format("REMARK   3   %s %g\n", "ANGLE RMSD                 : ", getRMSD()));
301     return sb.toString();
302   }
303 
304   @Override
305   public String toString() {
306     return format("  %s %20.8f %12d %12.3f (%8.5f)\n", "Angle Bending     ",
307         getEnergy(), getNumberOfAngles(), getTime(), getRMSD());
308   }
309 
310 }