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 edu.rit.pj.ParallelTeam;
48  import ffx.numerics.atomic.AtomicDoubleArray3D;
49  import ffx.potential.bonded.Atom;
50  import ffx.potential.nonbonded.GeneralizedKirkwood;
51  import java.util.logging.Level;
52  import java.util.logging.Logger;
53  
54  /**
55   * Parallel summation and reduction of components of the induced dipole field at each atom.
56   *
57   * @author Michael J. Schnieders
58   * @since 1.0
59   */
60  public class InducedDipoleFieldReduceRegion extends ParallelRegion {
61  
62    private static final Logger logger = Logger.getLogger(DirectRegion.class.getName());
63    private final InducedDipoleFieldReduceLoop[] inducedDipoleFieldReduceLoop;
64    /** Dimensions of [nsymm][nAtoms][3] */
65    public double[][][] inducedDipole;
66  
67    public double[][][] inducedDipoleCR;
68    /** An ordered array of atoms in the system. */
69    private Atom[] atoms;
70  
71    private double[][] cartesianDipolePhi;
72    private double[][] cartesianDipolePhiCR;
73    /** Field array. */
74    private AtomicDoubleArray3D field;
75    /** Chain rule field array. */
76    private AtomicDoubleArray3D fieldCR;
77    /** Flag to indicate use of generalized Kirkwood. */
78    private boolean generalizedKirkwoodTerm;
79  
80    private GeneralizedKirkwood generalizedKirkwood;
81    private double aewald;
82    private double aewald3;
83  
84    private double dielectric;
85  
86    public InducedDipoleFieldReduceRegion(int nt) {
87      inducedDipoleFieldReduceLoop = new InducedDipoleFieldReduceLoop[nt];
88    }
89  
90    /**
91     * Execute the InducedDipoleFieldReduceRegion with the passed ParallelTeam.
92     *
93     * @param parallelTeam The ParallelTeam instance to execute with.
94     */
95    public void executeWith(ParallelTeam parallelTeam) {
96      try {
97        parallelTeam.execute(this);
98      } catch (Exception e) {
99        String message = " Exception computing induced dipole field.\n";
100       logger.log(Level.WARNING, message, e);
101     }
102   }
103 
104   public void init(
105       Atom[] atoms,
106       double[][][] inducedDipole,
107       double[][][] inducedDipoleCR,
108       boolean generalizedKirkwoodTerm,
109       GeneralizedKirkwood generalizedKirkwood,
110       EwaldParameters ewaldParameters,
111       double dielectric,
112       double[][] cartesianDipolePhi,
113       double[][] cartesianDipolePhiCR,
114       AtomicDoubleArray3D field,
115       AtomicDoubleArray3D fieldCR) {
116     // Input
117     this.atoms = atoms;
118     this.inducedDipole = inducedDipole;
119     this.inducedDipoleCR = inducedDipoleCR;
120     this.generalizedKirkwoodTerm = generalizedKirkwoodTerm;
121     this.generalizedKirkwood = generalizedKirkwood;
122     this.aewald = ewaldParameters.aewald;
123     this.aewald3 = ewaldParameters.aewald3;
124     this.dielectric = dielectric;
125     this.cartesianDipolePhi = cartesianDipolePhi;
126     this.cartesianDipolePhiCR = cartesianDipolePhiCR;
127     // Output
128     this.field = field;
129     this.fieldCR = fieldCR;
130   }
131 
132   @Override
133   public void run() throws Exception {
134     try {
135       int threadID = getThreadIndex();
136       if (inducedDipoleFieldReduceLoop[threadID] == null) {
137         inducedDipoleFieldReduceLoop[threadID] = new InducedDipoleFieldReduceLoop();
138       }
139       int nAtoms = atoms.length;
140       execute(0, nAtoms - 1, inducedDipoleFieldReduceLoop[threadID]);
141     } catch (Exception e) {
142       String message =
143           " Fatal exception computing the mutual induced dipoles in thread "
144               + getThreadIndex()
145               + "\n";
146       logger.log(Level.SEVERE, message, e);
147     }
148   }
149 
150   private class InducedDipoleFieldReduceLoop extends IntegerForLoop {
151 
152     @Override
153     public void run(int lb, int ub) throws Exception {
154       int threadID = getThreadIndex();
155 
156       final double[][] induced0 = inducedDipole[0];
157       final double[][] inducedCR0 = inducedDipoleCR[0];
158       // Add the PME self and reciprocal space fields to the real space field.
159       if (aewald > 0.0) {
160         for (int i = lb; i <= ub; i++) {
161           double[] dipolei = induced0[i];
162           double[] dipoleCRi = inducedCR0[i];
163           final double[] phii = cartesianDipolePhi[i];
164           final double[] phiCRi = cartesianDipolePhiCR[i];
165           double fx = aewald3 * dipolei[0] - phii[t100];
166           double fy = aewald3 * dipolei[1] - phii[t010];
167           double fz = aewald3 * dipolei[2] - phii[t001];
168           double fxCR = aewald3 * dipoleCRi[0] - phiCRi[t100];
169           double fyCR = aewald3 * dipoleCRi[1] - phiCRi[t010];
170           double fzCR = aewald3 * dipoleCRi[2] - phiCRi[t001];
171           field.add(threadID, i, fx, fy, fz);
172           fieldCR.add(threadID, i, fxCR, fyCR, fzCR);
173         }
174       }
175 
176       // Reduce the total direct field.
177       field.reduce(lb, ub);
178       fieldCR.reduce(lb, ub);
179 
180       // Scale the total direct field by the inverse dielectric.
181       if (dielectric > 1.0) {
182         double inverseDielectric = 1.0 / dielectric;
183         for (int i = lb; i <= ub; i++) {
184           field.scale(0, i, inverseDielectric);
185           fieldCR.scale(0, i, inverseDielectric);
186         }
187       }
188 
189       // Add the GK reaction field.
190       if (generalizedKirkwoodTerm) {
191         AtomicDoubleArray3D fieldGK = generalizedKirkwood.getFieldGK();
192         AtomicDoubleArray3D fieldGKCR = generalizedKirkwood.getFieldGKCR();
193         for (int i = lb; i <= ub; i++) {
194           field.add(0, i, fieldGK.getX(i), fieldGK.getY(i), fieldGK.getZ(i));
195           fieldCR.add(0, i, fieldGKCR.getX(i), fieldGKCR.getY(i), fieldGKCR.getZ(i));
196         }
197       }
198     }
199 
200     @Override
201     public IntegerSchedule schedule() {
202       return IntegerSchedule.fixed();
203     }
204   }
205 }