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.numerics.Potential;
41 import ffx.potential.AssemblyState;
42 import ffx.potential.MolecularAssembly;
43
44 import java.util.logging.Logger;
45
46 import static java.lang.String.format;
47
48 /**
49 * The MolecularMC class is a framework to take Monte Carlo steps on a molecular system. It does not
50 * implement an MC algorithm, nor does it implement move sets; it is used to evaluate a single MC
51 * step with movements defined by implementations of MCMove.
52 *
53 * @author Michael J. Schnieders
54 * @author Jacob M. Litman
55 * @since 1.0
56 */
57 public class MolecularMC extends BoltzmannMC {
58
59 private static final Logger logger = Logger.getLogger(MolecularMC.class.getName());
60
61 /** The MolecularAssembly to operate on. */
62 private final MolecularAssembly molecularAssembly;
63
64 /** The potential energy for the molecular assembly. */
65 private final Potential potential;
66
67 /** Atomic coordinates. */
68 private double[] x;
69
70 /** Initial state of the MolecularAssembly. */
71 private AssemblyState initialState;
72
73 /**
74 * Constructs a DefaultMC instance with a molecular assembly and its PotentialEnergy. Fancy
75 * footwork will be required if we ever need to use multiple assemblies at once.
76 *
77 * @param molecularAssembly MolecularAssembly to operate on.
78 */
79 public MolecularMC(MolecularAssembly molecularAssembly) {
80 this(molecularAssembly, molecularAssembly.getPotentialEnergy());
81 }
82
83 /**
84 * Constructs a DefaultMC instance with a molecular assembly and a specific Potential.
85 *
86 * @param molecularAssembly MolecularAssembly to operate on.
87 * @param potential a {@link ffx.numerics.Potential} object.
88 */
89 public MolecularMC(MolecularAssembly molecularAssembly, Potential potential) {
90 this.molecularAssembly = molecularAssembly;
91 this.potential = potential;
92 }
93
94 /**
95 * Returns the associated MolecularAssembly.
96 *
97 * @return MolecularAssembly
98 */
99 public MolecularAssembly getMolecularAssembly() {
100 return molecularAssembly;
101 }
102
103 /**
104 * Returns the associated Potential.
105 *
106 * @return Potential.
107 */
108 public Potential getPotential() {
109 return potential;
110 }
111
112 /** {@inheritDoc} */
113 @Override
114 public void revertStep() {
115 initialState.revertState();
116 }
117
118 /** {@inheritDoc} */
119 @Override
120 public String toString() {
121 return "Default Metropolis Monte Carlo implementation\nTemperature: " + getTemperature()
122 + format("\ne1: %10.6f e2: %10.6f\nMolecular Assembly", getE1(), getE2())
123 + molecularAssembly.toString() + "\nPotential: " + potential.toString();
124 }
125
126 /**
127 * {@inheritDoc}
128 *
129 * <p>Calculates the energy at the current state; identical to RotamerOptimization method of same
130 * name.
131 */
132 @Override
133 protected double currentEnergy() {
134 if (x == null) {
135 int nVar = potential.getNumberOfVariables();
136 x = new double[nVar * 3];
137 }
138 try {
139 potential.getCoordinates(x);
140 return potential.energy(x);
141 } catch (ArithmeticException ex) {
142 logger.warning(ex.getMessage());
143 return 1e100;
144 }
145 }
146
147 /** {@inheritDoc} */
148 @Override
149 protected void storeState() {
150 initialState = new AssemblyState(molecularAssembly);
151 }
152 }