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