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