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