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.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   * Parallel initialization of accumulation arrays for Generalized Kirkwood.
56   *
57   * @author Michael J. Schnieders
58   * @since 1.0
59   */
60  public class InitializationRegion extends ParallelRegion {
61  
62    private static final Logger logger = Logger.getLogger(InitializationRegion.class.getName());
63  
64    /**
65     * Reference to the GK instance so that parameters can be updated.
66     */
67    private GeneralizedKirkwood generalizedKirkwood;
68    /**
69     * Initialization loops.
70     */
71    private final InitializationLoop[] initializationLoop;
72    /** Array of atoms. */
73    private Atom[] atoms;
74    /** True if GK is being turned on/off. */
75    private boolean lambdaTerm;
76    /** Atomic Gradient array. */
77    private AtomicDoubleArray3D grad;
78    /** Atomic Torque array. */
79    private AtomicDoubleArray3D torque;
80    /** Shared array for computation of Born radii gradient. */
81    private AtomicDoubleArray sharedBornGrad;
82  
83    public InitializationRegion(int maxThreads) {
84      initializationLoop = new InitializationLoop[maxThreads];
85    }
86  
87    /**
88     * Execute the InitializationRegion with the passed ParallelTeam.
89     *
90     * @param parallelTeam The ParallelTeam instance to execute with.
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           // Update GK parameters.
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 }