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