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.CustomCompoundBondForce;
44  import ffx.potential.bonded.PiOrbitalTorsion;
45  import ffx.potential.parameters.PiOrbitalTorsionType;
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 java.lang.String.format;
52  
53  /**
54   * OpenMM Pi-Orbital Torsion Force.
55   */
56  public class PiOrbitalTorsionForce extends CustomCompoundBondForce {
57  
58    private static final Logger logger = Logger.getLogger(PiOrbitalTorsionForce.class.getName());
59  
60    /**
61     * Create an Pi-Orbital Torsion Force.
62     *
63     * @param openMMEnergy The OpenMM Energy instance that contains the out-of-plane bends.
64     */
65    public PiOrbitalTorsionForce(OpenMMEnergy openMMEnergy) {
66      super(6, openMMEnergy.getPiOrbitalTorsionEnergyString());
67      PiOrbitalTorsion[] piOrbitalTorsions = openMMEnergy.getPiOrbitalTorsions();
68      if (piOrbitalTorsions == null || piOrbitalTorsions.length < 1) {
69        return;
70      }
71  
72      addPerBondParameter("k");
73      setName("PiOrbitalTorsion");
74  
75      IntArray particles = new IntArray(0);
76      DoubleArray parameters = new DoubleArray(0);
77      for (PiOrbitalTorsion piOrbitalTorsion : piOrbitalTorsions) {
78        int a1 = piOrbitalTorsion.getAtom(0).getXyzIndex() - 1;
79        int a2 = piOrbitalTorsion.getAtom(1).getXyzIndex() - 1;
80        int a3 = piOrbitalTorsion.getAtom(2).getXyzIndex() - 1;
81        int a4 = piOrbitalTorsion.getAtom(3).getXyzIndex() - 1;
82        int a5 = piOrbitalTorsion.getAtom(4).getXyzIndex() - 1;
83        int a6 = piOrbitalTorsion.getAtom(5).getXyzIndex() - 1;
84        PiOrbitalTorsionType type = piOrbitalTorsion.piOrbitalTorsionType;
85        double k = OpenMM_KJPerKcal * type.forceConstant * piOrbitalTorsion.piOrbitalTorsionType.piTorsUnit;
86        particles.append(a1);
87        particles.append(a2);
88        particles.append(a3);
89        particles.append(a4);
90        particles.append(a5);
91        particles.append(a6);
92        parameters.append(k);
93        addBond(particles, parameters);
94        particles.resize(0);
95        parameters.resize(0);
96      }
97      particles.destroy();
98      parameters.destroy();
99  
100     int forceGroup = openMMEnergy.getMolecularAssembly().getForceField().getInteger("PI_ORBITAL_TORSION_FORCE_GROUP", 0);
101     setForceGroup(forceGroup);
102     logger.log(Level.INFO, format("  Pi-Orbital Torsions  \t%6d\t\t%1d", piOrbitalTorsions.length, forceGroup));
103   }
104 
105   /**
106    * Convenience method to construct an OpenMM Pi-Orbital Torsion Force.
107    *
108    * @param openMMEnergy The OpenMM Energy instance that contains the pi-orbital torsions.
109    * @return An OpenMM Pi-Orbital Torsion Force, or null if there are no pi-orbital torsions.
110    */
111   public static Force constructForce(OpenMMEnergy openMMEnergy) {
112     PiOrbitalTorsion[] piOrbitalTorsions = openMMEnergy.getPiOrbitalTorsions();
113     if (piOrbitalTorsions == null || piOrbitalTorsions.length < 1) {
114       return null;
115     }
116     return new PiOrbitalTorsionForce(openMMEnergy);
117   }
118 
119   /**
120    * Update the Pi-Orbital Torsion force.
121    *
122    * @param openMMEnergy The OpenMM Energy instance that contains the pi-orbital torsions.
123    */
124   public void updateForce(OpenMMEnergy openMMEnergy) {
125     PiOrbitalTorsion[] piOrbitalTorsions = openMMEnergy.getPiOrbitalTorsions();
126     if (piOrbitalTorsions == null || piOrbitalTorsions.length < 1) {
127       return;
128     }
129 
130     IntArray particles = new IntArray(0);
131     DoubleArray parameters = new DoubleArray(0);
132     int index = 0;
133     for (PiOrbitalTorsion piOrbitalTorsion : piOrbitalTorsions) {
134       int a1 = piOrbitalTorsion.getAtom(0).getXyzIndex() - 1;
135       int a2 = piOrbitalTorsion.getAtom(1).getXyzIndex() - 1;
136       int a3 = piOrbitalTorsion.getAtom(2).getXyzIndex() - 1;
137       int a4 = piOrbitalTorsion.getAtom(3).getXyzIndex() - 1;
138       int a5 = piOrbitalTorsion.getAtom(4).getXyzIndex() - 1;
139       int a6 = piOrbitalTorsion.getAtom(5).getXyzIndex() - 1;
140       PiOrbitalTorsionType type = piOrbitalTorsion.piOrbitalTorsionType;
141       double k = OpenMM_KJPerKcal * type.forceConstant * piOrbitalTorsion.piOrbitalTorsionType.piTorsUnit;
142       particles.append(a1);
143       particles.append(a2);
144       particles.append(a3);
145       particles.append(a4);
146       particles.append(a5);
147       particles.append(a6);
148       parameters.append(k);
149       setBondParameters(index++, particles, parameters);
150       particles.resize(0);
151       parameters.resize(0);
152     }
153     particles.destroy();
154     parameters.destroy();
155     updateParametersInContext(openMMEnergy.getContext());
156   }
157 }