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.nonbonded.pme;
39  
40  import static ffx.potential.parameters.MultipoleType.t001;
41  import static ffx.potential.parameters.MultipoleType.t010;
42  import static ffx.potential.parameters.MultipoleType.t100;
43  
44  import edu.rit.pj.IntegerForLoop;
45  import edu.rit.pj.IntegerSchedule;
46  import edu.rit.pj.ParallelRegion;
47  import ffx.numerics.atomic.AtomicDoubleArray3D;
48  import ffx.potential.bonded.Atom;
49  import ffx.potential.nonbonded.GeneralizedKirkwood;
50  import java.util.logging.Level;
51  import java.util.logging.Logger;
52  
53  /**
54   * Parallel computation of the OPT induced dipoles.
55   *
56   * @author Michael J. Schnieders
57   * @since 1.0
58   */
59  public class OPTRegion extends ParallelRegion {
60  
61    private static final Logger logger = Logger.getLogger(OPTRegion.class.getName());
62    public final double[] optCoefficients;
63    /** Induced dipoles for extrapolated perturbation theory. */
64    public final int optOrder = 2;
65  
66    private final OPTLoop[] optLoop;
67    private final double[] optCoefficientsSum;
68    /** Dimensions of [nsymm][nAtoms][3] */
69    public double[][][] inducedDipole;
70  
71    public double[][][] inducedDipoleCR;
72    public double[][][] optDipole;
73    public double[][][] optDipoleCR;
74    /** An ordered array of atoms in the system. */
75    private Atom[] atoms;
76  
77    private double[] polarizability;
78    private double[][] cartesianDipolePhi;
79    private double[][] cartesianDipolePhiCR;
80    /** Field array. */
81    private AtomicDoubleArray3D field;
82    /** Chain rule field array. */
83    private AtomicDoubleArray3D fieldCR;
84    /** Flag to indicate use of generalized Kirkwood. */
85    private boolean generalizedKirkwoodTerm;
86  
87    private GeneralizedKirkwood generalizedKirkwood;
88    private double aewald;
89    private double aewald3;
90    private double dielectric;
91    private int currentOptOrder;
92  
93    public OPTRegion(int nt) {
94      optLoop = new OPTLoop[nt];
95      optCoefficients = new double[optOrder + 1];
96      optCoefficientsSum = new double[optOrder + 1];
97      switch (optOrder) {
98        case 1:
99          optCoefficients[0] = 0.530;
100         optCoefficients[1] = 0.604;
101         break;
102       case 2:
103         optCoefficients[0] = 0.042;
104         optCoefficients[1] = 0.635;
105         optCoefficients[2] = 0.414;
106         break;
107       case 3:
108         optCoefficients[0] = -0.132;
109         optCoefficients[1] = 0.218;
110         optCoefficients[2] = 0.637;
111         optCoefficients[3] = 0.293;
112         break;
113       case 4:
114         optCoefficients[0] = -0.071;
115         optCoefficients[1] = -0.096;
116         optCoefficients[2] = 0.358;
117         optCoefficients[3] = 0.587;
118         optCoefficients[4] = 0.216;
119         break;
120       case 5:
121         optCoefficients[0] = -0.005;
122         optCoefficients[1] = -0.129;
123         optCoefficients[2] = -0.026;
124         optCoefficients[3] = 0.465;
125         optCoefficients[4] = 0.528;
126         optCoefficients[5] = 0.161;
127         break;
128       case 6:
129         optCoefficients[0] = 0.014;
130         optCoefficients[1] = -0.041;
131         optCoefficients[2] = -0.172;
132         optCoefficients[3] = 0.073;
133         optCoefficients[4] = 0.535;
134         optCoefficients[5] = 0.467;
135         optCoefficients[6] = 0.122;
136         break;
137       default:
138         logger.severe(" Unsupported OPT order.");
139     }
140 
141     for (int i = 0; i <= optOrder; i++) {
142       for (int j = optOrder; j >= i; j--) {
143         optCoefficientsSum[i] += optCoefficients[j];
144       }
145     }
146   }
147 
148   public void init(
149       int currentOptOrder,
150       Atom[] atoms,
151       double[] polarizability,
152       double[][][] inducedDipole,
153       double[][][] inducedDipoleCR,
154       double[][] cartesianDipolePhi,
155       double[][] cartesianDipolePhiCR,
156       AtomicDoubleArray3D field,
157       AtomicDoubleArray3D fieldCR,
158       boolean generalizedKirkwoodTerm,
159       GeneralizedKirkwood generalizedKirkwood,
160       EwaldParameters ewaldParameters,
161       double dielectric) {
162     this.currentOptOrder = currentOptOrder;
163     this.atoms = atoms;
164     this.polarizability = polarizability;
165     this.inducedDipole = inducedDipole;
166     this.inducedDipoleCR = inducedDipoleCR;
167     this.cartesianDipolePhi = cartesianDipolePhi;
168     this.cartesianDipolePhiCR = cartesianDipolePhiCR;
169     this.field = field;
170     this.fieldCR = fieldCR;
171     this.generalizedKirkwoodTerm = generalizedKirkwoodTerm;
172     this.generalizedKirkwood = generalizedKirkwood;
173     this.aewald = ewaldParameters.aewald;
174     this.aewald3 = ewaldParameters.aewald3;
175     this.dielectric = dielectric;
176   }
177 
178   @Override
179   public void run() throws Exception {
180     try {
181       int ti = getThreadIndex();
182       if (optLoop[ti] == null) {
183         optLoop[ti] = new OPTRegion.OPTLoop();
184       }
185       int nAtoms = atoms.length;
186       execute(0, nAtoms - 1, optLoop[ti]);
187     } catch (RuntimeException ex) {
188       logger.warning(
189           "Fatal exception computing the opt induced dipoles in thread " + getThreadIndex());
190       throw ex;
191     } catch (Exception e) {
192       String message =
193           "Fatal exception computing the opt induced dipoles in thread " + getThreadIndex() + "\n";
194       logger.log(Level.SEVERE, message, e);
195     }
196   }
197 
198   private class OPTLoop extends IntegerForLoop {
199 
200     @Override
201     public void run(int lb, int ub) throws Exception {
202       int threadID = getThreadIndex();
203 
204       final double[][] induced0 = inducedDipole[0];
205       final double[][] inducedCR0 = inducedDipoleCR[0];
206       if (aewald > 0.0) {
207         // Add the self and reciprocal space fields to the real space field.
208         for (int i = lb; i <= ub; i++) {
209           double[] dipolei = induced0[i];
210           double[] dipoleCRi = inducedCR0[i];
211           final double[] phii = cartesianDipolePhi[i];
212           final double[] phiCRi = cartesianDipolePhiCR[i];
213           double fx = aewald3 * dipolei[0] - phii[t100];
214           double fy = aewald3 * dipolei[1] - phii[t010];
215           double fz = aewald3 * dipolei[2] - phii[t001];
216           double fxCR = aewald3 * dipoleCRi[0] - phiCRi[t100];
217           double fyCR = aewald3 * dipoleCRi[1] - phiCRi[t010];
218           double fzCR = aewald3 * dipoleCRi[2] - phiCRi[t001];
219           field.add(threadID, i, fx, fy, fz);
220           fieldCR.add(threadID, i, fxCR, fyCR, fzCR);
221         }
222       }
223 
224       // Reduce the total intramolecular field.
225       field.reduce(lb, ub);
226       fieldCR.reduce(lb, ub);
227 
228       // Scale the total intramolecular field by the inverse solute dielectric.
229       if (dielectric > 1.0) {
230         double inverseDielectric = 1.0 / dielectric;
231         for (int i = lb; i <= ub; i++) {
232           field.scale(0, i, inverseDielectric);
233           fieldCR.scale(0, i, inverseDielectric);
234         }
235       }
236 
237       if (generalizedKirkwoodTerm) {
238         AtomicDoubleArray3D fieldGK = generalizedKirkwood.getFieldGK();
239         AtomicDoubleArray3D fieldGKCR = generalizedKirkwood.getFieldGKCR();
240         // Add the GK reaction field to the intramolecular field.
241         for (int i = lb; i <= ub; i++) {
242           field.add(0, i, fieldGK.getX(i), fieldGK.getY(i), fieldGK.getZ(i));
243           fieldCR.add(0, i, fieldGKCR.getX(i), fieldGKCR.getY(i), fieldGKCR.getZ(i));
244         }
245       }
246 
247       // Collect the current Opt Order induced dipole.
248       for (int i = lb; i <= ub; i++) {
249         final double[] ind = induced0[i];
250         final double[] indCR = inducedCR0[i];
251         final double polar = polarizability[i];
252         for (int j = 0; j < 3; j++) {
253           optDipole[currentOptOrder][i][j] = polar * field.get(j, i);
254           optDipoleCR[currentOptOrder][i][j] = polar * fieldCR.get(j, i);
255           ind[j] = optDipole[currentOptOrder][i][j];
256           indCR[j] = optDipoleCR[currentOptOrder][i][j];
257         }
258       }
259     }
260 
261     @Override
262     public IntegerSchedule schedule() {
263       return IntegerSchedule.fixed();
264     }
265   }
266 }