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;
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 RowRegion 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 "row" of 3D grid (i.e. fixed values of the z and y-coordinates) 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 RowRegion extends ParallelRegion {
64  
65    public int buff = 3;
66    public boolean[][] select;
67    protected RowLoop[] rowLoop;
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[][][] zyAtListBuild;
79  
80    /**
81     * Constructor for RowRegion.
82     *
83     * @param gX a int.
84     * @param gY a int.
85     * @param gZ a int.
86     * @param grid the grid array.
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 the atomic coordinate array.
91     */
92    public RowRegion(int gX, int gY, int gZ, double[] grid,
93        int nSymm, int threadCount, Atom[] atoms, double[][][] coordinates) {
94      this.nAtoms = atoms.length;
95      this.gX = gX;
96      this.gY = gY;
97      this.gZ = gZ;
98      gridSize = gX * gY * gZ * 2;
99      this.nSymm = nSymm;
100     this.coordinates = coordinates;
101     this.grid = grid;
102     if (grid != null) {
103       gridBuffer = DoubleBuffer.wrap(grid);
104     }
105     rowLoop = new RowLoop[threadCount];
106     gridInitLoop = new GridInitLoop[threadCount];
107     for (int i = 0; i < threadCount; i++) {
108       gridInitLoop[i] = new GridInitLoop();
109     }
110     select = new boolean[nSymm][nAtoms];
111     for (int i = 0; i < nSymm; i++) {
112       fill(select[i], true);
113     }
114     zyAtListBuild = new int[nSymm][nAtoms][2];
115     rebuildList = true;
116   }
117 
118   /** {@inheritDoc} */
119   @Override
120   public void finish() {
121     if (rebuildList) {
122       rowLoop[0].saveZYValues(zyAtListBuild);
123     }
124     rebuildList = false;
125   }
126 
127   /**
128    * Getter for the field <code>grid</code>.
129    *
130    * @return the grid array.
131    */
132   public double[] getGrid() {
133     return grid;
134   }
135 
136   /**
137    * getNatoms.
138    *
139    * @return a int.
140    */
141   public int getNatoms() {
142     return nAtoms;
143   }
144 
145   /**
146    * getNsymm.
147    *
148    * @return a int.
149    */
150   public int getNsymm() {
151     return nSymm;
152   }
153 
154   /**
155    * rowIndexForYZ.
156    *
157    * @param giy a int.
158    * @param giz a int.
159    * @return a int.
160    */
161   public int rowIndexForYZ(int giy, int giz) {
162     return giy + gY * giz;
163   }
164 
165   /** {@inheritDoc} */
166   @Override
167   public void run() throws Exception {
168     int threadIndex = getThreadIndex();
169     RowLoop loop = rowLoop[threadIndex];
170     // This lets the same SpatialDensityLoops be used with different SpatialDensityRegions.
171     loop.setNsymm(nSymm);
172     try {
173       execute(0, gridSize - 1, gridInitLoop[threadIndex]);
174       execute(0, (gY * gZ) - 1, loop);
175     } catch (Exception e) {
176       String message = " Exception in RowRegion.";
177       logger.log(Level.SEVERE, message, e);
178     }
179   }
180 
181   /**
182    * Select atoms that should be included. The default is to include all atoms, which is set up in
183    * the constructor. This function should be over-ridden by subclasses that want finer control.
184    */
185   public void selectAtoms() {
186     for (int i = 0; i < nSymm; i++) {
187       fill(select[i], true);
188     }
189   }
190 
191   /**
192    * Setter for the field <code>atoms</code>.
193    *
194    * @param atoms an array of {@link ffx.potential.bonded.Atom} objects.
195    */
196   public void setAtoms(Atom[] atoms) {
197     nAtoms = atoms.length;
198     select = new boolean[nSymm][nAtoms];
199     for (int i = 0; i < nSymm; i++) {
200       fill(select[i], true);
201     }
202   }
203 
204   /**
205    * Setter for the field <code>crystal</code>.
206    *
207    * @param crystal a {@link ffx.crystal.Crystal} object.
208    * @param gX a int.
209    * @param gY a int.
210    * @param gZ a int.
211    */
212   public final void setCrystal(Crystal crystal, int gX, int gY, int gZ) {
213     // this.crystal = crystal.getUnitCell();
214     this.gX = gX;
215     this.gY = gY;
216     this.gZ = gZ;
217     gridSize = gX * gY * gZ * 2;
218   }
219 
220   /**
221    * setDensityLoop.
222    *
223    * @param loops an array of {@link ffx.potential.nonbonded.RowLoop} objects.
224    */
225   public void setDensityLoop(RowLoop[] loops) {
226     rowLoop = loops;
227   }
228 
229   /**
230    * Setter for the field <code>initValue</code>.
231    *
232    * @param initValue a double.
233    */
234   public void setInitValue(double initValue) {
235     this.initValue = initValue;
236   }
237 
238   /** {@inheritDoc} */
239   @Override
240   public void start() {
241     selectAtoms();
242     rebuildList = (rebuildList || rowLoop[0].checkList(zyAtListBuild, buff));
243   }
244 
245   /**
246    * yFromRowIndex.
247    *
248    * @param i a int.
249    * @return a int.
250    */
251   public int yFromRowIndex(int i) {
252     return i % gY;
253   }
254 
255   /**
256    * zFromRowIndex.
257    *
258    * @param i a int.
259    * @return a int.
260    */
261   public int zFromRowIndex(int i) {
262     return i / gY;
263   }
264 
265   /**
266    * Setter for the field <code>gridBuffer</code>.
267    *
268    * @param grid a {@link java.nio.DoubleBuffer} object.
269    */
270   void setGridBuffer(DoubleBuffer grid) {
271     gridBuffer = grid;
272   }
273 
274   private class GridInitLoop extends IntegerForLoop {
275 
276     private final IntegerSchedule schedule = IntegerSchedule.fixed();
277 
278     @Override
279     public void run(int lb, int ub) {
280       if (gridBuffer != null) {
281         // if (grid != null) {
282         for (int i = lb; i <= ub; i++) {
283           // grid[i] = initValue;
284           gridBuffer.put(i, initValue);
285         }
286       }
287     }
288 
289     @Override
290     public IntegerSchedule schedule() {
291       return schedule;
292     }
293   }
294 }