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.potential.nonbonded.implicit;
39
40 import edu.rit.pj.IntegerForLoop;
41 import edu.rit.pj.IntegerSchedule;
42 import edu.rit.pj.ParallelRegion;
43 import edu.rit.pj.ParallelTeam;
44 import ffx.numerics.atomic.AtomicDoubleArray;
45 import ffx.numerics.atomic.AtomicDoubleArray3D;
46 import ffx.potential.bonded.Atom;
47 import ffx.potential.nonbonded.GeneralizedKirkwood;
48
49 import java.util.logging.Level;
50 import java.util.logging.Logger;
51
52 import static java.lang.String.format;
53
54
55
56
57
58
59
60 public class InitializationRegion extends ParallelRegion {
61
62 private static final Logger logger = Logger.getLogger(InitializationRegion.class.getName());
63
64
65
66
67 private GeneralizedKirkwood generalizedKirkwood;
68
69
70
71 private final InitializationLoop[] initializationLoop;
72
73 private Atom[] atoms;
74
75 private boolean lambdaTerm;
76
77 private AtomicDoubleArray3D grad;
78
79 private AtomicDoubleArray3D torque;
80
81 private AtomicDoubleArray sharedBornGrad;
82
83 public InitializationRegion(int maxThreads) {
84 initializationLoop = new InitializationLoop[maxThreads];
85 }
86
87
88
89
90
91
92 public void executeWith(ParallelTeam parallelTeam) {
93 try {
94 parallelTeam.execute(this);
95 } catch (Exception e) {
96 String message = " Exception expanding initializing GK.\n";
97 logger.log(Level.SEVERE, message, e);
98 }
99 }
100
101 public void init(
102 GeneralizedKirkwood generalizedKirkwood,
103 Atom[] atoms,
104 boolean lambdaTerm,
105 AtomicDoubleArray3D grad,
106 AtomicDoubleArray3D torque,
107 AtomicDoubleArray sharedBornGrad) {
108 this.generalizedKirkwood = generalizedKirkwood;
109 this.atoms = atoms;
110 this.lambdaTerm = lambdaTerm;
111 this.grad = grad;
112 this.torque = torque;
113 this.sharedBornGrad = sharedBornGrad;
114 }
115
116 @Override
117 public void run() {
118 int threadIndex = getThreadIndex();
119 if (initializationLoop[threadIndex] == null) {
120 initializationLoop[threadIndex] = new InitializationLoop();
121 }
122 try {
123 int nAtoms = atoms.length;
124 execute(0, nAtoms - 1, initializationLoop[threadIndex]);
125 } catch (Exception e) {
126 String message = "Fatal exception initializing coordinates in thread: " + threadIndex + "\n";
127 logger.log(Level.SEVERE, message, e);
128 }
129 }
130
131 private class InitializationLoop extends IntegerForLoop {
132
133 private int threadID;
134
135 @Override
136 public void run(int lb, int ub) {
137 grad.reset(threadID, lb, ub);
138 torque.reset(threadID, lb, ub);
139 sharedBornGrad.reset(threadID, lb, ub);
140 if (lambdaTerm) {
141 for (int i = lb; i <= ub; i++) {
142
143 generalizedKirkwood.udpateSoluteParameters(i);
144
145 if (!atoms[i].applyLambda()) {
146 logger.warning(format(" Atom %s is not alchemical.", atoms[i].toString()));
147 logger.warning(" Alchemical GK calculations require all atoms to be alchemical.");
148 break;
149 }
150 }
151 }
152 }
153
154 @Override
155 public IntegerSchedule schedule() {
156 return IntegerSchedule.fixed();
157 }
158
159 @Override
160 public void start() {
161 threadID = getThreadIndex();
162 }
163 }
164 }