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.algorithms.optimize.manybody;
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.Residue;
45  import java.util.Arrays;
46  
47  public class EnergyRegion extends ParallelRegion {
48  
49    private EnergyExpansion eE;
50    /** Flag to control use of 3-body terms. */
51    private boolean threeBodyTerm;
52  
53    private final SharedDouble self;
54    private final SharedDouble twoBody;
55    private final SharedDouble threeBody;
56    private final EnergyLoop[] energyLoops;
57    private Residue[] residues;
58    private int[] rotamers;
59    private int nResidues;
60  
61    public EnergyRegion(int nThreads) {
62      self = new SharedDouble();
63      twoBody = new SharedDouble();
64      threeBody = new SharedDouble();
65      energyLoops = new EnergyLoop[nThreads];
66    }
67  
68    public double getSelf() {
69      return self.get();
70    }
71  
72    public double getThreeBody() {
73      return threeBody.get();
74    }
75  
76    public double getTwoBody() {
77      return twoBody.get();
78    }
79  
80    public void init(EnergyExpansion eE, Residue[] residues, int[] rotamers, boolean threeBodyTerm) {
81      this.eE = eE;
82      this.rotamers = rotamers;
83      this.nResidues = residues.length;
84      this.residues = Arrays.copyOf(residues, nResidues);
85      this.threeBodyTerm = threeBodyTerm;
86    }
87  
88    @Override
89    public void run() throws Exception {
90      int threadID = getThreadIndex();
91      if (energyLoops[threadID] == null) {
92        energyLoops[threadID] = new EnergyLoop();
93      }
94      execute(0, nResidues - 1, energyLoops[threadID]);
95    }
96  
97    public void start() {
98      self.set(0.0);
99      twoBody.set(0.0);
100     threeBody.set(0.0);
101   }
102 
103   private class EnergyLoop extends IntegerForLoop {
104 
105     private double selfSum;
106     private double pairSum;
107     private double threeBodySum;
108 
109     @Override
110     public void finish() {
111       self.addAndGet(selfSum);
112       twoBody.addAndGet(pairSum);
113       threeBody.addAndGet(threeBodySum);
114     }
115 
116     @Override
117     public void run(int lb, int ub) {
118       for (int a = lb; a <= ub; a++) {
119         int ai = rotamers[a];
120         selfSum += eE.getSelf(a, ai);
121         for (int b = a + 1; b < nResidues; b++) {
122           int bi = rotamers[b];
123           pairSum += eE.get2Body(a, ai, b, bi);
124           if (threeBodyTerm) {
125             for (int c = b + 1; c < nResidues; c++) {
126               int ci = rotamers[c];
127               threeBodySum += eE.get3Body(residues, a, ai, b, bi, c, ci);
128             }
129           }
130         }
131       }
132     }
133 
134     @Override
135     public IntegerSchedule schedule() {
136       return IntegerSchedule.dynamic();
137     }
138 
139     @Override
140     public void start() {
141       selfSum = 0.0;
142       pairSum = 0.0;
143       threeBodySum = 0.0;
144     }
145   }
146 }