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.algorithms.mc;
39
40 import static org.apache.commons.math3.util.FastMath.abs;
41 import static org.apache.commons.math3.util.FastMath.max;
42 import static org.apache.commons.math3.util.FastMath.min;
43
44 import ffx.algorithms.thermodynamics.OrthogonalSpaceTempering;
45 import java.util.Random;
46 import java.util.logging.Logger;
47
48 /**
49 * Define an MC move to update lambda.
50 *
51 * @author Mallory R. Tollefson
52 */
53 public class LambdaMove implements MCMove {
54
55 private static final Logger logger = Logger.getLogger(LambdaMove.class.getName());
56 /** Apply the Lambda move to an OST instance. */
57 private final OrthogonalSpaceTempering orthogonalSpaceTempering;
58 /** Random number generator. */
59 private final Random random;
60 /** Current value of lambda, which always refreshed from the OST instance. */
61 private double currentLambda;
62 /**
63 * Lambda move size: 1) The standard deviation for continuous moves from a Gaussian distribution.
64 * 2) The step size for discrete moves.
65 */
66 private double moveSize = 0.1;
67 /** If true, do continuous moves. Otherwise, use discrete moves. */
68 private boolean isContinuous = true;
69
70 /**
71 * Constructor for LambdaMove.
72 *
73 * @param orthogonalSpaceTempering a {@link OrthogonalSpaceTempering} object.
74 */
75 public LambdaMove(OrthogonalSpaceTempering orthogonalSpaceTempering) {
76 this.orthogonalSpaceTempering = orthogonalSpaceTempering;
77 random = new Random();
78 }
79
80 /**
81 * Constructor for LambdaMove.
82 *
83 * @param randomSeed Random seed to use.
84 * @param orthogonalSpaceTempering OrthogonalSpaceTempering instance.
85 */
86 public LambdaMove(int randomSeed, OrthogonalSpaceTempering orthogonalSpaceTempering) {
87 this.orthogonalSpaceTempering = orthogonalSpaceTempering;
88 random = new Random(randomSeed);
89 }
90
91 /**
92 * Applies 0-1 mirroring conditions to lam + dL. Skips any moves where dL is greater than 1 or less
93 * than -1, and skips 50% of moves from 0 or 1 (exact).
94 *
95 * @param random Source of randomness.
96 * @param lam Initial lambda.
97 * @param dL Change in lambda.
98 * @return Correctly mirrored lam + dL
99 */
100 public static double mirror(Random random, double lam, double dL) {
101 if (lam == 0.0 || lam == 1.0) {
102 boolean skip = random.nextBoolean();
103 if (skip) {
104 return lam;
105 }
106 }
107 // Eliminate really weird edge cases.
108 if (abs(dL) > 1.0) {
109 logger.warning(String.format(" Skipping large lambda move of %.3f not between -1 and +1", dL));
110 return lam;
111 }
112 // Math.abs to mirror negative values.
113 double newLam = abs(lam + dL);
114 // If greater than 1, mirror via 2.0 - val
115 return newLam <= 1.0 ? newLam : 2.0 - newLam;
116 }
117
118 /**
119 * Get the Lambda move size, which is a standard deviation for continuous moves or step size for
120 * discrete moves.
121 *
122 * @return The lambda move size.
123 */
124 public double getMoveSize() {
125 return moveSize;
126 }
127
128 /**
129 * Get the Lambda move size, which is a standard deviation for continuous moves or step size for
130 * discrete moves.
131 *
132 * @param moveSize a double.
133 */
134 public void setMoveSize(double moveSize) {
135 this.moveSize = moveSize;
136 }
137
138 /**
139 * If true, do continuous moves. Otherwise, use discrete moves.
140 *
141 * @return Returns true if the lambda moves are continuous.
142 */
143 public boolean isContinuous() {
144 return isContinuous;
145 }
146
147 /**
148 * If true, do continuous moves. Otherwise, use discrete moves.
149 *
150 * @param continuous Sets the lambda move style.
151 */
152 public void setContinuous(boolean continuous) {
153 isContinuous = continuous;
154 }
155
156 /** {@inheritDoc} */
157 @Override
158 public void move() {
159 currentLambda = orthogonalSpaceTempering.getLambda();
160
161 // Draw a trial move from the distribution.
162 double dL = isContinuous ? continuousMove() : discreteMove();
163 double newLambda = mirror(currentLambda, dL);
164
165 // Update the OST instance.
166 orthogonalSpaceTempering.setLambda(newLambda);
167 }
168
169 /** {@inheritDoc} */
170 @Override
171 public void revertMove() {
172 orthogonalSpaceTempering.setLambda(currentLambda);
173 }
174
175 /**
176 * Validate lambda is in the range [0 .. 1].
177 *
178 * <p>For discrete moves, set Lambda to the closest valid value [0, dL, 2dL, .. 1].
179 *
180 * @param lambda Input lambda value.
181 * @return Validated lambda value.
182 */
183 public double validateLambda(double lambda) {
184 lambda = max(0.0, min(lambda, 1.0));
185 if (isContinuous) {
186 return lambda;
187 }
188 double remainder = lambda % moveSize;
189 if (remainder < moveSize / 2.0) {
190 return max(0.0, lambda - remainder);
191 } else {
192 return min(lambda + (moveSize - remainder), 1.0);
193 }
194 }
195
196 /**
197 * Applies 0-1 mirroring conditions to lam + dL. Skips any moves where dL is greater than 1 or less
198 * than -1, and skips 50% of moves from 0 or 1 (exact).
199 *
200 * @param lam Initial lambda.
201 * @param dL Change in lambda.
202 * @return Correctly mirrored lam + dL
203 */
204 private double mirror(double lam, double dL) {
205 // Telescope to public static method because a public static method
206 // may be useful in the future.
207 return mirror(random, lam, dL);
208 }
209
210 /**
211 * Pulls a delta-lambda from a continuous Gaussian distribution.
212 *
213 * @return A random Gaussian value with width of moveSize.
214 */
215 private double continuousMove() {
216 // Draw a trial move from the distribution.
217 return random.nextGaussian() * moveSize;
218 }
219
220 /**
221 * Pulls a continuous lambda move of width moveSize.
222 *
223 * @return +/- moveSize (never 0)
224 */
225 private double discreteMove() {
226 // Make a discrete move.
227 double dL = moveSize;
228 if (random.nextBoolean()) {
229 dL = -moveSize;
230 }
231 return dL;
232 }
233 }