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.BondedTerm;
41  import ffx.potential.bonded.UreyBradley;
42  
43  import java.util.ArrayList;
44  import java.util.Collection;
45  import java.util.Collections;
46  import java.util.List;
47  import java.util.logging.Logger;
48  import static java.lang.String.format;
49  
50  /**
51   * Urey-Bradley potential energy term using {@link ffx.potential.bonded.UreyBradley} instances.
52   *
53   * @author Michael J. Schnieders
54   * @since 1.0
55   */
56  public class UreyBradleyPotentialEnergy extends EnergyTerm {
57  
58    private static final Logger logger = Logger.getLogger(UreyBradleyPotentialEnergy.class.getName());
59  
60  
61    /**
62     * Internal list of UreyBradley instances.
63     */
64    private final List<UreyBradley> ureyBradleys = new ArrayList<>();
65  
66    /**
67     * Create a UreyBradleyPotentialEnergy with the provided name.
68     *
69     * @param name Name for this term.
70     */
71    public UreyBradleyPotentialEnergy(String name) {
72      super(name);
73    }
74  
75    /**
76     * Create a UreyBradleyPotentialEnergy with the provided name and force group.
77     *
78     * @param name       Name for this term.
79     * @param forceGroup Integer force group identifier.
80     */
81    public UreyBradleyPotentialEnergy(String name, int forceGroup) {
82      super(name, forceGroup);
83    }
84  
85    /**
86     * Create a UreyBradleyPotentialEnergy initialized with a list of UreyBradleys and force group.
87     *
88     * @param name         Name for this term.
89     * @param forceGroup   Force group identifier.
90     * @param ureyBradleys List of UreyBradley instances to add (null-safe).
91     */
92    public UreyBradleyPotentialEnergy(String name, int forceGroup, List<UreyBradley> ureyBradleys) {
93      super(name, forceGroup);
94      if (ureyBradleys != null) {
95        Collections.sort(ureyBradleys);
96        this.ureyBradleys.addAll(ureyBradleys);
97        logger.info(format("  Urey-Bradleys:                     %10d", getNumberOfUreyBradleys()));
98      }
99    }
100 
101   /**
102    * Get the number of terms in this potential energy term.
103    *
104    * @return The number of terms, which is the same as the number of Urey-Bradley interactions.
105    */
106   @Override
107   public int getNumberOfTerms() {
108     return getNumberOfUreyBradleys();
109   }
110 
111   /**
112    * Get an array of BondedTerms in this term.
113    *
114    * @return Array of BondedTerms, which are actually UreyBradleys in this case.
115    */
116   @Override
117   public BondedTerm[] getBondedTermsArray() {
118     return getUreyBradleyArray();
119   }
120 
121   /**
122    * Create a UreyBradleyPotentialEnergy initialized with a collection of UreyBradleys.
123    *
124    * @param name         Name for this term (may be null).
125    * @param ureyBradleys Collection of UreyBradley instances to add (null-safe).
126    */
127   public UreyBradleyPotentialEnergy(String name, Collection<UreyBradley> ureyBradleys) {
128     super(name);
129     if (ureyBradleys != null) {
130       this.ureyBradleys.addAll(ureyBradleys);
131     }
132   }
133 
134   /**
135    * Add a UreyBradley to this term.
136    *
137    * @param ureyBradley UreyBradley to add (ignored if null).
138    * @return true if the UreyBradley was added.
139    */
140   public boolean addUreyBradley(UreyBradley ureyBradley) {
141     if (ureyBradley == null) {
142       return false;
143     }
144     return ureyBradleys.add(ureyBradley);
145   }
146 
147   /**
148    * Add an array of UreyBradleys to this term.
149    *
150    * @param ureyBradleys Array of UreyBradley instances to add.
151    * @return true if the UreyBradleys were added.
152    */
153   public boolean addUreyBradleys(UreyBradley[] ureyBradleys) {
154     if (ureyBradleys == null) {
155       return false;
156     }
157     Collections.addAll(this.ureyBradleys, ureyBradleys);
158     return true;
159   }
160 
161   /**
162    * Add a list of UreyBradleys to this term.
163    *
164    * @param ureyBradleys List of UreyBradley instances to add.
165    * @return true if the UreyBradleys were added.
166    */
167   public boolean addUreyBradleys(List<UreyBradley> ureyBradleys) {
168     if (ureyBradleys == null) {
169       return false;
170     }
171     this.ureyBradleys.addAll(ureyBradleys);
172     return true;
173   }
174 
175   /**
176    * Remove a UreyBradley from this term.
177    *
178    * @param ureyBradley UreyBradley to remove (ignored if null).
179    * @return true if the UreyBradley was present and removed.
180    */
181   public boolean removeUreyBradley(UreyBradley ureyBradley) {
182     if (ureyBradley == null) {
183       return false;
184     }
185     return ureyBradleys.remove(ureyBradley);
186   }
187 
188   /**
189    * Get the UreyBradley at a given index.
190    *
191    * @param index Index in the internal list.
192    * @return UreyBradley at the specified index.
193    * @throws IndexOutOfBoundsException if index is invalid.
194    */
195   public UreyBradley getUreyBradley(int index) {
196     return ureyBradleys.get(index);
197   }
198 
199   /**
200    * Get an unmodifiable view of the UreyBradleys in this term.
201    *
202    * @return Unmodifiable List of UreyBradleys.
203    */
204   public List<UreyBradley> getUreyBradleys() {
205     return Collections.unmodifiableList(ureyBradleys);
206   }
207 
208   /**
209    * Get an array of UreyBradleys in this term.
210    *
211    * @return Array of UreyBradleys.
212    */
213   public UreyBradley[] getUreyBradleyArray() {
214     return ureyBradleys.toArray(new UreyBradley[0]);
215   }
216 
217   /**
218    * Get the number of UreyBradleys in this term.
219    *
220    * @return The number of UreyBradleys.
221    */
222   public int getNumberOfUreyBradleys() {
223     return ureyBradleys.size();
224   }
225 
226   /**
227    * Log the details of Urey-Bradley interactions.
228    */
229   @Override
230   public void log() {
231     if (getNumberOfUreyBradleys() <= 0) {
232       return;
233     }
234     logger.info("\n Urey-Bradley Interactions:");
235     for (UreyBradley ureyBradley : getUreyBradleys()) {
236       logger.info("Urey-Bradley \t" + ureyBradley.toString());
237     }
238   }
239 
240   @Override
241   public String toPDBString() {
242     if (getNumberOfUreyBradleys() <= 0) {
243       return "";
244     }
245     return format("REMARK   3   %s %g (%d)\n", "UREY-BRADLEY               : ", getEnergy(), getNumberOfUreyBradleys());
246   }
247 
248   @Override
249   public String toString() {
250     return format("  %s %20.8f %12d %12.3f\n", "Urey-Bradley      ",
251         getEnergy(), getNumberOfUreyBradleys(), getTime());
252   }
253 
254 }