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.potential.openmm;
39  
40  import ffx.openmm.DoubleArray;
41  import ffx.openmm.Force;
42  import ffx.openmm.IntArray;
43  import ffx.openmm.CentroidBondForce;
44  import ffx.potential.bonded.Atom;
45  import ffx.potential.nonbonded.RestrainGroups;
46  
47  import java.util.logging.Level;
48  import java.util.logging.Logger;
49  
50  import static edu.uiowa.jopenmm.OpenMMAmoebaLibrary.OpenMM_KJPerKcal;
51  import static edu.uiowa.jopenmm.OpenMMAmoebaLibrary.OpenMM_NmPerAngstrom;
52  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Boolean.OpenMM_False;
53  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Boolean.OpenMM_True;
54  import static java.lang.String.format;
55  
56  /**
57   * Restrain Groups Force.
58   */
59  public class RestrainGroupsForce extends CentroidBondForce {
60  
61    private static final Logger logger = Logger.getLogger(RestrainGroupsForce.class.getName());
62  
63    private static final String energy = "step(distance(g1,g2)-u)*k*(distance(g1,g2)-u)^2+step(l-distance(g1,g2))*k*(distance(g1,g2)-l)^2";
64  
65    /**
66     * Restrain Groups Force constructor.
67     *
68     * @param openMMEnergy The OpenMM Energy.
69     */
70    public RestrainGroupsForce(OpenMMEnergy openMMEnergy) {
71      super(2, energy);
72      RestrainGroups restrainGroups = openMMEnergy.getRestrainGroups();
73      if (restrainGroups == null) {
74        destroy();
75        return;
76      }
77  
78      addPerBondParameter("k");
79      addPerBondParameter("l");
80      addPerBondParameter("u");
81  
82      Atom[] atoms = openMMEnergy.getMolecularAssembly().getAtomArray();
83  
84      // Create the Restrain Groups.
85      int nGroups = restrainGroups.getNumberOfGroups();
86      IntArray group = new IntArray(0);
87      DoubleArray weight = new DoubleArray(0);
88      for (int j = 0; j < nGroups; j++) {
89        int[] groupMembers = restrainGroups.getGroupMembers(j);
90        for (int i : groupMembers) {
91          group.append(i);
92          weight.append(atoms[i].getMass());
93        }
94        addGroup(group, weight);
95        group.resize(0);
96        weight.resize(0);
97      }
98      group.destroy();
99      weight.destroy();
100 
101     // Add the restraints between groups.
102     double convert = OpenMM_KJPerKcal / (OpenMM_NmPerAngstrom * OpenMM_NmPerAngstrom);
103     int nRestraints = restrainGroups.getNumberOfRestraints();
104     int[] group1 = restrainGroups.getGroup1();
105     int[] group2 = restrainGroups.getGroup2();
106     double[] forceConstants = restrainGroups.getForceConstants();
107     double[] smallerDistance = restrainGroups.getSmallerDistance();
108     double[] largerDistance = restrainGroups.getLargerDistance();
109     group = new IntArray(0);
110     DoubleArray parameters = new DoubleArray(0);
111     for (int i = 0; i < nRestraints; i++) {
112       group.append(group1[i]);
113       group.append(group2[i]);
114       parameters.append(forceConstants[i] * convert);
115       parameters.append(smallerDistance[i] * OpenMM_NmPerAngstrom);
116       parameters.append(largerDistance[i] * OpenMM_NmPerAngstrom);
117       addBond(group, parameters);
118       group.resize(0);
119       parameters.resize(0);
120     }
121     group.destroy();
122     parameters.destroy();
123 
124     // Add the constraint force.
125     int forceGroup = openMMEnergy.getMolecularAssembly().getForceField().getInteger("RESTRAIN_GROUPS_FORCE_GROUP", 0);
126     setForceGroup(forceGroup);
127 
128     if (openMMEnergy.getCrystal().aperiodic()) {
129       setUsesPeriodicBoundaryConditions(OpenMM_False);
130     } else {
131       setUsesPeriodicBoundaryConditions(OpenMM_True);
132     }
133     logger.log(Level.INFO, format("  Restrain Groups \t%6d\t\t%1d", nRestraints, forceGroup));
134   }
135 
136   /**
137    * Add a Restrain-Groups force to the OpenMM System.
138    */
139   public static Force constructForce(OpenMMEnergy openMMEnergy) {
140     RestrainGroups restrainGroups = openMMEnergy.getRestrainGroups();
141     if (restrainGroups == null) {
142       return null;
143     }
144     return new RestrainGroupsForce(openMMEnergy);
145   }
146 }