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-2025.
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 edu.rit.pj.IntegerForLoop;
41  import edu.rit.pj.IntegerSchedule;
42  import edu.rit.pj.ParallelRegion;
43  import edu.rit.pj.reduction.SharedDouble;
44  import ffx.potential.bonded.Atom;
45  
46  import java.util.logging.Level;
47  import java.util.logging.Logger;
48  
49  /**
50   * Parallel computation of the polarization energy as sum over atomic contributions (-1/2 u.E).
51   *
52   * @author Michael J. Schnieders
53   * @since 1.0
54   */
55  public class PolarizationEnergyRegion extends ParallelRegion {
56  
57    private static final Logger logger = Logger.getLogger(PolarizationEnergyRegion.class.getName());
58    private final double electric;
59    private final PolarizationEnergyLoop[] polarizationLoop;
60    private final SharedDouble polarizationEnergy = new SharedDouble();
61    /** An ordered array of atoms in the system. */
62    private Atom[] atoms;
63  
64    private double[] polarizability;
65    /** Dimensions of [nsymm][nAtoms][3] */
66    private double[][][] inducedDipole;
67    /** Direct induced dipoles. */
68    private double[][] directDipoleCR;
69  
70    private double polarizationScale;
71  
72    public PolarizationEnergyRegion(int nt, double electric) {
73      this.electric = electric;
74      polarizationLoop = new PolarizationEnergyLoop[nt];
75    }
76  
77    @Override
78    public void finish() {
79      double energy = polarizationEnergy.get();
80      polarizationEnergy.set(energy * polarizationScale * electric);
81    }
82  
83    /**
84     * Return the final polarization energy.
85     *
86     * @return The polarization energy.
87     */
88    public double getPolarizationEnergy() {
89      return polarizationEnergy.get();
90    }
91  
92    /**
93     * Set the current polarization energy.
94     *
95     * @param energy Value to set the polarization energy to.
96     */
97    public void setPolarizationEnergy(double energy) {
98      polarizationEnergy.set(energy);
99    }
100 
101   public void init(
102       Atom[] atoms,
103       double[] polarizability,
104       double[][][] inducedDipole,
105       double[][] directDipoleCR,
106       double polarizationScale) {
107     this.atoms = atoms;
108     this.polarizability = polarizability;
109     this.inducedDipole = inducedDipole;
110     this.directDipoleCR = directDipoleCR;
111     this.polarizationScale = polarizationScale;
112   }
113 
114   @Override
115   public void run() throws Exception {
116     try {
117       int ti = getThreadIndex();
118       if (polarizationLoop[ti] == null) {
119         polarizationLoop[ti] = new PolarizationEnergyLoop();
120       }
121       int nAtoms = atoms.length;
122       execute(0, nAtoms - 1, polarizationLoop[ti]);
123     } catch (RuntimeException ex) {
124       logger.warning(
125           "Runtime exception computing polarization energy in thread " + getThreadIndex());
126       throw ex;
127     } catch (Exception e) {
128       String message =
129           "Fatal exception computing polarization energy in thread " + getThreadIndex() + "\n";
130       logger.log(Level.SEVERE, message, e);
131     }
132   }
133 
134   @Override
135   public void start() {
136     polarizationEnergy.set(0.0);
137   }
138 
139   private class PolarizationEnergyLoop extends IntegerForLoop {
140 
141     @Override
142     public void run(int lb, int ub) throws Exception {
143       double energy = 0.0;
144       for (int i = lb; i <= ub; i++) {
145         if (polarizability[i] == 0.0) {
146           continue;
147         }
148         double uix = inducedDipole[0][i][0];
149         double uiy = inducedDipole[0][i][1];
150         double uiz = inducedDipole[0][i][2];
151         double pix = directDipoleCR[i][0];
152         double piy = directDipoleCR[i][1];
153         double piz = directDipoleCR[i][2];
154         energy += (uix * pix + uiy * piy + uiz * piz) / polarizability[i];
155       }
156       polarizationEnergy.addAndGet(-0.5 * energy);
157     }
158 
159     @Override
160     public IntegerSchedule schedule() {
161       return IntegerSchedule.fixed();
162     }
163   }
164 }