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