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