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;
39
40 import static ffx.potential.nonbonded.SpatialDensityRegion.logger;
41 import static java.util.Arrays.fill;
42
43 import edu.rit.pj.IntegerForLoop;
44 import edu.rit.pj.IntegerSchedule;
45 import edu.rit.pj.ParallelRegion;
46 import ffx.crystal.Crystal;
47 import ffx.potential.bonded.Atom;
48 import java.nio.DoubleBuffer;
49 import java.util.logging.Level;
50
51 /**
52 * The SliceLoop class is used to parallelize placing onto a 3D grid
53 *
54 * <p>1) Multipoles using B-splines or
55 *
56 * <p>2) Diffraction form factors.
57 *
58 * <p>Each "slice" of the grid (i.e. a fixed value of the z-coordinate) is operated on by only a
59 * single thread to logically enforce atomic updates of grid magnitudes.
60 *
61 * @author Armin Avdic
62 */
63 public class SliceRegion extends ParallelRegion {
64
65 public int buff = 3;
66 public boolean[][] select;
67 protected SliceLoop[] sliceLoop;
68 protected double[][][] coordinates;
69 int nAtoms;
70 int nSymm;
71 private int gX, gY, gZ;
72 private DoubleBuffer gridBuffer;
73 private GridInitLoop[] gridInitLoop;
74 private double initValue = 0.0;
75 private int gridSize;
76 private double[] grid;
77 private boolean rebuildList;
78 private int[][] zAtListBuild;
79
80 /**
81 * Constructor for SliceRegion.
82 *
83 * @param gX a int.
84 * @param gY a int.
85 * @param gZ a int.
86 * @param grid an array of {@link double} objects.
87 * @param nSymm a int.
88 * @param threadCount a int.
89 * @param atoms an array of {@link ffx.potential.bonded.Atom} objects.
90 * @param coordinates an array of {@link double} objects.
91 */
92 public SliceRegion(
93 int gX,
94 int gY,
95 int gZ,
96 double[] grid,
97 int nSymm,
98 int threadCount,
99 Atom[] atoms,
100 double[][][] coordinates) {
101 this.nAtoms = atoms.length;
102 this.gX = gX;
103 this.gY = gY;
104 this.gZ = gZ;
105 gridSize = gX * gY * gZ * 2;
106 this.nSymm = nSymm;
107 this.coordinates = coordinates;
108 this.grid = grid;
109 if (grid != null) {
110 gridBuffer = DoubleBuffer.wrap(grid);
111 }
112 sliceLoop = new SliceLoop[threadCount];
113 gridInitLoop = new GridInitLoop[threadCount];
114 for (int i = 0; i < threadCount; i++) {
115 gridInitLoop[i] = new GridInitLoop();
116 }
117 select = new boolean[nSymm][nAtoms];
118 for (int i = 0; i < nSymm; i++) {
119 fill(select[i], true);
120 }
121 zAtListBuild = new int[nSymm][nAtoms];
122 rebuildList = true;
123 }
124
125 /** {@inheritDoc} */
126 @Override
127 public void finish() {
128 if (rebuildList) {
129 sliceLoop[0].saveZValues(zAtListBuild);
130 }
131 rebuildList = false;
132 }
133
134 /**
135 * Getter for the field <code>grid</code>.
136 *
137 * @return an array of {@link double} objects.
138 */
139 public double[] getGrid() {
140 return grid;
141 }
142
143 /**
144 * getNatoms.
145 *
146 * @return a int.
147 */
148 public int getNatoms() {
149 return nAtoms;
150 }
151
152 /**
153 * getNsymm.
154 *
155 * @return a int.
156 */
157 public int getNsymm() {
158 return nSymm;
159 }
160
161 /** {@inheritDoc} */
162 @Override
163 public void run() throws Exception {
164 int threadIndex = getThreadIndex();
165 SliceLoop loop = sliceLoop[threadIndex];
166 // This lets the same SpatialDensityLoops be used with different SpatialDensityRegions.
167 loop.setNsymm(nSymm);
168 loop.setRebuildList(rebuildList);
169 try {
170 execute(0, gridSize - 1, gridInitLoop[threadIndex]);
171 execute(0, gZ - 1, loop);
172 } catch (Exception e) {
173 String message = " Exception in SliceRegion.";
174 logger.log(Level.SEVERE, message, e);
175 }
176 }
177
178 /**
179 * Select atoms that should be included. The default is to include all atoms, which is set up in
180 * the constructor. This function should be over-ridden by subclasses that want finer control.
181 */
182 public void selectAtoms() {
183 for (int i = 0; i < nSymm; i++) {
184 fill(select[i], true);
185 }
186 }
187
188 /**
189 * Setter for the field <code>atoms</code>.
190 *
191 * @param atoms an array of {@link ffx.potential.bonded.Atom} objects.
192 */
193 public void setAtoms(Atom[] atoms) {
194 nAtoms = atoms.length;
195 select = new boolean[nSymm][nAtoms];
196 zAtListBuild = new int[nSymm][nAtoms];
197 for (int i = 0; i < nSymm; i++) {
198 fill(select[i], true);
199 }
200 rebuildList = true;
201 }
202
203 /**
204 * Setter for the field <code>crystal</code>.
205 *
206 * @param crystal a {@link ffx.crystal.Crystal} object.
207 * @param gX a int.
208 * @param gY a int.
209 * @param gZ a int.
210 */
211 public final void setCrystal(Crystal crystal, int gX, int gY, int gZ) {
212 // this.crystal = crystal.getUnitCell();
213 this.gX = gX;
214 this.gY = gY;
215 this.gZ = gZ;
216 gridSize = gX * gY * gZ * 2;
217 }
218
219 /**
220 * setDensityLoop.
221 *
222 * @param loops an array of {@link ffx.potential.nonbonded.SliceLoop} objects.
223 */
224 public void setDensityLoop(SliceLoop[] loops) {
225 sliceLoop = loops;
226 }
227
228 /**
229 * Setter for the field <code>initValue</code>.
230 *
231 * @param initValue a double.
232 */
233 public void setInitValue(double initValue) {
234 this.initValue = initValue;
235 }
236
237 /** {@inheritDoc} */
238 @Override
239 public void start() {
240 selectAtoms();
241 rebuildList = (rebuildList || sliceLoop[0].checkList(zAtListBuild, buff));
242 }
243
244 /**
245 * Setter for the field <code>gridBuffer</code>.
246 *
247 * @param grid a {@link java.nio.DoubleBuffer} object.
248 */
249 void setGridBuffer(DoubleBuffer grid) {
250 gridBuffer = grid;
251 }
252
253 private class GridInitLoop extends IntegerForLoop {
254
255 private final IntegerSchedule schedule = IntegerSchedule.fixed();
256
257 @Override
258 public void run(int lb, int ub) {
259 if (gridBuffer != null) {
260 // if (grid != null) {
261 for (int i = lb; i <= ub; i++) {
262 // grid[i] = initValue;
263 gridBuffer.put(i, initValue);
264 }
265 }
266 }
267
268 @Override
269 public IntegerSchedule schedule() {
270 return schedule;
271 }
272 }
273 }